just a quick question am abit rubish with regex so thought I would post on here. The regex below is to validate a username.
-
Must be between 4-26 characters long
-
Start with atleast 2 letters
- Can only contain numbers and one
underscore and one dot
I have this so far, but isn’t working
<?php
$username=$_POST['username'];
if (!eregi("^([a-zA-Z][0-9_.]){4,26}$",$username))
{
return false;
}
else
{
echo "username ok";
}
?>
Thanks 🙂
You could use the regex
as in
^(?=[a-z]{2})ensure the string “Start with atleast 2 letters”.(?=.{4,26})ensure it “Must be between 4-26 characters long”.(?=[^.]*\.?[^.]*$)ensures the following characters contains at most one.until the end.(?=[^_]*_?[^_]*$)ensures at most one_.[\w.]+$commits the match. It also ensures only alphanumerics,_and.will be involved.(Note: this regex assumes
hello_worldis a valid user name.)