开发者

validation for php [duplicate]

开发者 https://www.devze.com 2023-01-27 07:29 出处:网络
This question already has answers here: 开发者_如何学CClosed 12 years ago. Possible Duplicate: Is there a php library for email address validation?
This question already has answers here: 开发者_如何学C Closed 12 years ago.

Possible Duplicate:

Is there a php library for email address validation?

How can I create a validation for email address?


use filter

<?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

0

精彩评论

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