cescentman
asked on
Matching - newbie to regular expressions
I want to parse the input from a form so that it only contains upper or lower letters, numbers and hyphens. How do write such an expression?
if (isset($_POST['frmDomName'])) {
if ('contains something other that legal l characters') {
header('Location: http://mydom.com/Failure.php');
}
}
hello,
to match upper or lower letters, numbers and hyphens, use
if (preg_match('/[A-Za-z\-]/' , $_POST['frmDomName']))
to match upper or lower letters, numbers and hyphens, use
if (preg_match('/[A-Za-z\-]/'
This should work fine:
if (isset($_POST['frmDomName'])) {
if (!ereg("^([0-9]|[a-z]|[A-Z]|_)+$", $_POST['frmDomName'], $reg)) {
header('Location: http://mydom.com/Failure.php');
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This work Just fine, but it should be noted that the ! is required for the particular logic I outlined.
if (!preg_match('/^[A-Za-z0-9\-]*$/', $_POST['frmDomName'])) {
header('Location: http://mydom.com/Failure.php');
}
cescentman,
Yes, you're right.
Yes, you're right.
Like:
if (preg_match('/[A-Za-z0-9]/
{
header('Location: http://mydom.com/Failure.php');
}