Link to home
Start Free TrialLog in
Avatar of cescentman
cescentmanFlag for United Kingdom of Great Britain and Northern Ireland

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');
	}
}

Open in new window

Avatar of Ovunc Tukenmez
Ovunc Tukenmez
Flag of Türkiye image

Add all charachers that you want:
Like:

if (preg_match('/[A-Za-z0-9]/', $_POST['frmDomName']))
{
                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']))
Avatar of xBellox
xBellox

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');
        }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ovunc Tukenmez
Ovunc Tukenmez
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cescentman

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');
}

Open in new window

cescentman,
Yes, you're right.