TechplexEngineer
TechplexEngineer

Reputation: 1928

PHP and regular expressions, Why isn't this working?

if(strlen($_REQUEST['email']) >= 0 && preg_match('^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$', $_REQUEST['email']))
{
    $error = true;
    echo "Please enter a valid Email address";
}

Warning: preg_match() [function.preg-match]: No ending delimiter '^' found in /var/www/team2648/OPIS/register.php on line 30

Upvotes: 0

Views: 53

Answers (3)

Crozin
Crozin

Reputation: 44376

PHP comes with built-in function for validating some popular data like URLs, emails etc. - it's called filter_var().

if ($_REQUEST['email'] && filter_var($_REQUEST['email'], FILTER_VALIDATE_EMAIL)) {
    // This makes your code much cleaner!
}

Upvotes: 1

Matti Virkkunen
Matti Virkkunen

Reputation: 65126

You forgot the delimiters. Add them.

Also note that you regex ignores some perfectly valid addresses (such as [email protected])

Upvotes: 4

Andrew
Andrew

Reputation: 14447

preg_match requires opening and closing delimiters like perl. You want to include, say, a slash at the beginning and ending of the regex string like

'/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'

Upvotes: 3

Related Questions