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
What could be the Regular Expression for Validating a user's name?
Collapse
X
-
Tags: None
-
Use preg_match to match your user name with the required expression.Originally posted by rizwan6febThis 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
In your case it is[php]if (preg_match("/^[a-zA-Z]+$/", $user_name))
{
//matching
}
else
{
//not matching
}[/php]
Regular Expression Details -
Just Use Trim() and check to see if its blank if its not blank, then check it against the regex [a-zA-Z\s?]+Originally posted by vikas1111Hi
Also check for blank space entry.. Instead of name i can enter blank space also,, Checking for only alphabets woth work properly..
Good luck,
DanComment
-
What if it's in the middle of the string? Trim() clears the beggining and end, no?Originally posted by dlite922Just 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
The regex:trim — Strip whitespace (or other characters) from the beginning and end of a string
[php]
if(preg_match('/[^a-zA-Z]/', $string)
{
// bad name
}
else
{
// good name
}
[/php]
Should do it.Comment
Comment