Quote:
|
Originally Posted by kvnband
I'm still learning regular expressions, but I know that
is for a-z, A-Z, 0-9 but I'm not sure how you add a hyphen in there.
|
What's wrong with above code:
^[a-zA-Z0-9]$ -> will only evalute true for single character not many.
Quote:
|
Originally Posted by keith
To allow upper & lowercase letters, numbers, spaces, hyphens, underscores, and periods, I use:
|
What's wrong with above code:
1. A-z should be A-Z or a-z
2. The operator ^ in square bracket [] will behave as negation and not assertion. Thus ^A-Z means you disallow alphabet A through Z
3. The dot (.) refers to all characters except newline, if you want to include the dot itself, cast it first using \
4. As with kvnband, the regex will only evaluate single character.
Valid regular expression should be:
$isMatch = preg_match('/^[a-zA-Z_-\.]+$/',$username);
or
$isMatch = preg_match('/^[a-z_-\.]+$/i',$username);
or
$isMatch = preg_match('/^[a-z_\-\.]+$/i',$username); //safely cast dash
Notes:
1.It's much better to use preg_match than ereg because it will evaluate faster (you may give a try for speed test)
2.Before using the regex test function, trim the trailing spaces by using php function trim()