-1

In my program I have a account ID and I want to create a username. It is cannot contain numeric values at the start of their name unless those numeric numbers are equal to their account ID number. For example if I am seller ID 44 and I try to create username “37verifier” it is not fine. But if I try to create 44Verifier then it is fine. If I try to create “A37verifier”, again this is fine. And also allowed only "@ and .(dot)" special character in name (if it is email id format), not allowed other special characters in my username.

How can I do it in a php script? Please help me.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
naresh
  • 174
  • 2
  • 12
  • Try to find more info for 'Regular expressions in PHP' – webo80 Nov 09 '15 at 08:24
  • An emailaddress allows more special characters than `@` and `.` : http://stackoverflow.com/questions/2049502/what-characters-are-allowed-in-email-address – AgeDeO Nov 09 '15 at 08:26

1 Answers1

0

Use regular expressions and check if the username checks it.

Assuming we already have two variables, $username is the string to check and $ID is the user ID.

$regexp="/^(".$ID."|[a-z@\.]+)[a-z0-9@\.]+$/i";
if (preg_match($regexp,$username))
{
    //... do something
}else{
    die("Invalid username");
}

This will match if:

  • The user name starts with the user ID
  • The user name starts with a letter, dot or @ character

This will not match if the user nae starts with a number different than the user ID.

However, this wont check if the provided usernameis a valid email address.

ojovirtual
  • 3,332
  • 16
  • 21