开发者

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

if(strlen开发者_StackOverflow($_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


You forgot the delimiters. Add them.

Also note that you regex ignores some perfectly valid addresses (such as foo@example.museum)


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}$/'


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!
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜