Page 1 of 1
register form - valid username
Posted: Sat Nov 06, 2004 10:25 am
by pimp
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
Posted: Sat Nov 06, 2004 10:54 am
by swdev
I am not very good at regular expressions , but take a look at this code
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');
?>
Hope this helps
Posted: Sat Nov 06, 2004 11:36 am
by timvw
Posted: Sun Nov 07, 2004 1:59 pm
by pimp
thank you for helping. timvw: i understand now the expressions but how do i use it in ereg ? if i only have 2 parameters what is the value that it's returned ?
ex: echo ([a-zA-Z],"heLLo1")
Posted: Sun Nov 07, 2004 2:47 pm
by John Cartwright
how do i use it in ereg
Use preg_match as shown in swdev's post.
Posted: Mon Nov 08, 2004 2:58 am
by pimp
ok. thanks a lot