register form - valid username
Moderator: General Moderators
register form - valid username
hello. i have a problem regarding validating the username from a registration form. i want the username to consist only of a...z, A...Z, and _ . i can't understand the manual for ereg, please help me by any way you can. thanks
I am not very good at regular expressions , but take a look at this code
Hope this helps
Code: Select all
<?php
function ValidateUsername($UserName = '')
{
// ^ - start at the beginning of the line
// a-z - match any character in the range a-z i.e. all lowercase characters
// A-Z - match any character in the range A-Z i.e. all uppercase characters
// _ - match the underscore character
// [...] match any character that is specified between the open and close bracket
// * match 0 or more characters
// $ - finish at the end of the line
$match = '/^([a-zA-Z_])*$/';
if (1 == preg_match ($match, $UserName) )
{
echo $UserName . ' is valid<br />';
$ret = true;
}
else
{
echo $UserName . ' is <strong>invalid</strong><br />';
$ret = false;
}
}
// test words
ValidateUsername ('Hello_world');
ValidateUsername ('A Bad Username 99');
?>the shortcut would be to match on \w
http://www.regular-expressions.info/reference.html
something to train your re skillz:
http://www.samuelfullman.com/team/php/t ... ster_p.php
http://www.regular-expressions.info/reference.html
something to train your re skillz:
http://www.samuelfullman.com/team/php/t ... ster_p.php
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact: