How to validate a username using php
My rules are:
Username can only contain letters, numbers and 1 underscore.
This is my current preg_match
preg_match('/^[a-zA-Z0-9][_]{1}[a-zA-Z0-9]+$/',$_POST['uname'])
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This should do it:
Keep in mind that a character class of a single item is redundant. Write the atom solely instead. And
{1}is another seriously redundant quantifier.is just the same as
Edit:
Based on the new constrains and in the helpful comment by chris, this seems to be a better regex:
It matches usernames of at least two character, not ending or beginning with the optional underscore, and no newlines at the end.