0

dear all,
i am facing a problem. actually i wanna get user validation with the following code, I want to insert only [a-z0-9_] character in user login....when i insert username like this 'asdfdfad' it returns else part ( special characters are not allowed ). please solve it ..... thanks

if ( preg_match ('/^[a-z0-9]$/', $username)) {
    echo $username. ' ' . $password . ' ' . $email;     
}
else {
    echo 'special characters are not allowed';
}
2
Contributors
1
Reply
14
Views
3 Years
Discussion Span
Last Post by Isaac_4
2

Your pattern matches only a single character, add a + to match one or more characters:

if ( preg_match ('/^[a-z0-9]+$/', $username)) {
    echo $username. ' ' . $password . ' ' . $email;     
}
else {
    echo 'special characters are not allowed';
}

I suggest using a tool like RegexBuddy to help write and test regular expressions.

Edited by Isaac_4

Votes + Comments
+1 for RegexBuddy
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.