PHP的filter_var FILTER_VALIDATE_EMAIL实际上工作吗?
阅读各种职位后,我决定不使用REGEX来检查电子邮件是否有效,只需使用PHP的内置filter_var函数。 这似乎工作正常,直到它开始告诉我一封电子邮件是无效的,因为我有一个数字。
即name@domain.com工作,而name2@domain.com不工作。
我错过了什么,或者是filter_var($email, FILTER_VALIDATE_EMAIL)
真的很无效吗?
PHP 5.3.3filter代码中使用的正则expression式基于Michael Rushton关于电子邮件地址validation的博客。 它似乎适用于你提到的情况。
您还可以查看比较电子邮件地址validation正则expression式中的一些选项(PHP中正在使用的正则expression式是其中一个testing)。
然后,您可以select一个更好的正则expression式,并在调用preg_match()
使用它。
否则,你可以采取正则expression式,并replace文件PHP / ext / filter / logical_filter.c,函数php_filter_validate_email()
,并重新php_filter_validate_email()
PHP。
name2@domain.com似乎工作正常: http ://codepad.org/5HDgMW5i
但是我确实看到有人抱怨说它有问题,即使在SO上也是如此。 很可能,它确实有问题,但正则expression式解决scheme也是如此。 电子邮件地址规范非常非常复杂( RFC XXXX )。
这就是为什么validation电子邮件的唯一方法是发送电子邮件到地址和需求操作(例如:如果是注册脚本,请求他们点击validation链接)。
该filter最近已经改造。 http://codepad.org/Lz5m2S2N – 出现在键盘使用的版本,你的情况是正确过滤
你也可以看看http://bugs.php.net/49576和http://svn.php.net/viewvc/php/php-src/trunk/ext/filter/logical_filters.c 。 正则expression式是相当可怕的。
function isValidEmail($email, $checkDNS = false) { $valid = ( /* Preference for native version of function */ function_exists('filter_var') and filter_var($email, FILTER_VALIDATE_EMAIL) ) || ( /* The maximum length of an e-mail address is 320 octets, per RFC 2821. */ strlen($email) <= 320 /* * The regex below is based on a regex by Michael Rushton. * However, it is not identical. I changed it to only consider routeable * addresses as valid. Michael's regex considers a@ba valid address * which conflicts with section 2.3.5 of RFC 5321 which states that: * * Only resolvable, fully-qualified domain names (FQDNs) are permitted * when domain names are used in SMTP. In other words, names that can * be resolved to MX RRs or address (ie, A or AAAA) RRs (as discussed * in Section 5) are permitted, as are CNAME RRs whose targets can be * resolved, in turn, to MX or address RRs. Local nicknames or * unqualified names MUST NOT be used. * * This regex does not handle comments and folding whitespace. While * this is technically valid in an email address, these parts aren't * actually part of the address itself. */ and preg_match_all( '/^(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?))'. '{255,})(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?))'. '{65,}@)(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|'. '(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))'. '(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|'. '(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|'. '(?:\\x5C[\\x00-\\x7F]))*\\x22)))*@(?:(?:(?!.*[^.]{64,})'. '(?:(?:(?:xn--)?[a-z0-9]+(?:-+[a-z0-9]+)*\\.){1,126})'.'{1,}'. '(?:(?:[az][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-+[a-z0-9]+)*)|'. '(?:\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|'. '(?:(?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::'. '(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|'. '(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|'. '(?:(?!(?:.*[a-f0-9]:){5,})'.'(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::'. '(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|'. '(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?:(?:25[0-5])|'. '(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))$/iD', $email) ); if( $valid ) { if( $checkDNS && ($domain = end(explode('@',$email, 2))) ) { /* Note: Adding the dot enforces the root. The dot is sometimes necessary if you are searching for a fully qualified domain which has the same name as a host on your local domain. Of course the dot does not alter results that were OK anyway. */ return checkdnsrr($domain . '.', 'MX'); } return true; } return false; } //----------------------------------------------------------------- var_dump(isValidEmail('nechtan@tagon8inc.com', true)); // bool(true)
- 点击特定button时如何跳过validation?
- 可以在html5中validationinput = file的大小和types
- 如何将一个RequiredFieldValidator添加到DropDownList控件?
- 如何更新属性而无需validation
- 从Controller手动设置ModelState.isValid
- 用于客户端表单validation和交互的最佳JavaScript解决scheme?
- 如何限制EditText只接受字母数字字符
- Bad value X-UA-元素meta上属性http-equiv的兼容性
- 我怎样才能使用普通的HTTP身份validation和PHP在Apache下获得身份validation的用户名?