validation for php

Possible Duplicate:
Is there a php library for email address validation?

How can I create a validation for email address?


使用过滤器

<?php
$email_a = 'joe@example.com';
$email_b = 'bogus';

if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) {
    echo "This (email_a) email address is considered valid.";
}
if (filter_var($email_b, FILTER_VALIDATE_EMAIL)) {
    echo "This (email_b) email address is considered valid.";
}
?>

If you're looking for full control, you can test the email against a regular expression of your own requirements. You can accomplish this using PHP's preg_match() method.

Example:

<?php
   echo preg_match('/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+.([a-zA-Z])+([a-zA-Z])+$/', 'bob@example.com');
?>

If the email address is valid, preg_match will return 1. Otherwise, preg_match will return a value of 0.

-OR- Use PHP's built in filter:

<?php
   echo filter_var('bob@example.com', FILTER_VALIDATE_EMAIL);
?>

Of'course, I've seen many people state that FILTER_VALIDATE_EMAIL is not enough, and return to regular expressions.

You can learn more about PHP regular expressions here: http://php.net/manual/en/book.regex.php

链接地址: http://www.djcxy.com/p/92726.html

上一篇: 最好的电子邮件验证功能在一般和特定(学院领域)?

下一篇: 验证的PHP