Is it better to display an error if user input is invalid or automatically fix it?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Samishii23
    New Member
    • Sep 2009
    • 246

    #1

    Is it better to display an error if user input is invalid or automatically fix it?

    Making a login script.
    Should I throw an error if a user puts invalid characters into the form and submits it, or trim them automatically?
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    You should error and inform them *why*.

    Comment

    • Samishii23
      New Member
      • Sep 2009
      • 246

      #3
      Thanks. But now after a few hours of searching google... I haven't really been able to find a code example of this logic: If theres anything else BESIDES [a-zA-Z0-9] = true

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        That's simply done with a regular expression:

        Code:
        if (preg_match('/[^a-zA-Z0-9]/', $text))
        {
          // Found a non-alphanumeric character
        }
        The caret (^) in the above character-set means 'not in this character set'. So the above expression says 'match anything that is not a-z, A-Z or 0-9 - alphanumeric.

        Comment

        • Samishii23
          New Member
          • Sep 2009
          • 246

          #5
          Forgive the n00b questions. But what are the / for?

          Comment

          • JKing
            Recognized Expert Top Contributor
            • Jun 2007
            • 1206

            #6
            The / is the delimiter or start and end of the regex. You can use any non alpha numeric character as the delimiter. In some cases it may make more sense to use a % or other symbol if your regex contains several forward slashes then you don't have to escape all of them using a backslash.

            Comment

            Working...