What could be the Regular Expression for Validating a user's name?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rizwan6feb
    New Member
    • Jul 2007
    • 108

    What could be the Regular Expression for Validating a user's name?

    This may be very simple, but how can i check, whether a user's name (firstname or lastname) is valid or not? A user's name should contain only alphabets
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    Originally posted by rizwan6feb
    This may be very simple, but how can i check, whether a user's name (firstname or lastname) is valid or not? A user's name should contain only alphabets
    Use preg_match to match your user name with the required expression.
    In your case it is[php]if (preg_match("/^[a-zA-Z]+$/", $user_name))
    {
    //matching
    }
    else
    {
    //not matching
    }[/php]
    Regular Expression Details

    Comment

    • vikas1111
      New Member
      • Feb 2008
      • 122

      #3
      Hi
      Also check for blank space entry.. Instead of name i can enter blank space also,, Checking for only alphabets woth work properly..

      Comment

      • hsriat
        Recognized Expert Top Contributor
        • Jan 2008
        • 1653

        #4
        "/^[a-zA-Z\s]+$/"

        See if this works.

        Comment

        • dlite922
          Recognized Expert Top Contributor
          • Dec 2007
          • 1586

          #5
          Originally posted by vikas1111
          Hi
          Also check for blank space entry.. Instead of name i can enter blank space also,, Checking for only alphabets woth work properly..
          Just Use Trim() and check to see if its blank if its not blank, then check it against the regex [a-zA-Z\s?]+

          Good luck,


          Dan

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Originally posted by dlite922
            Just Use Trim() and check to see if its blank if its not blank, then check it against the regex [a-zA-Z\s?]+

            Good luck,


            Dan
            What if it's in the middle of the string? Trim() clears the beggining and end, no?

            trim — Strip whitespace (or other characters) from the beginning and end of a string
            The regex:
            [php]
            if(preg_match('/[^a-zA-Z]/', $string)
            {
            // bad name
            }
            else
            {
            // good name
            }
            [/php]
            Should do it.

            Comment

            Working...