我需要电子邮件验证方面的帮助,这个验证代码都是特定格式的电子邮件,比如,test@test.gov.au,test@something.ac.au,并且我希望它被允许为test@something.au格式。
(注:这里只允许我进入5个域,即gov.au、edu.au、govt.nz、ac.au和csiro.au)
我的代码如下
联署材料:
function emailTldValidation(tlds) {
$.validator.addMethod("emailTld", function(value,element) {
if (value.search("@") != -1) {
return (/(.+)@(.+)\.(gov\.au|edu\.au|ac\.nz|csiro\.au|govt\.nz)$/).test(value);
//return (/(.+)@(.+)\.(csiro\.au|gov|gov\.us)$/).test(value);
}
return false;
},"Please enter valid tld like "+tlds);
$.validator.addClassRules({
stringInput: {
emailTld: true
}
});
}下面的代码在function.php中得到验证
function validateEmail($email) {
//validate email here from server side
if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
//validate tld
$validTlds = str_replace(".", "\.", VALID_EMAIL_TLDS);
$validTlds = "\.".str_replace(",", "|\.", $validTlds);
$emailArr = explode("@", $email);
$emailTld = $emailArr[1];
if (preg_match('/^[-a-z0-9]+\.[a-z][a-z]|('.$validTlds.')\z/', strtolower($emailTld))) {
//check main domain here
$exValidTlds = explode(",", VALID_EMAIL_TLDS);
$exValidTlds = array_map('trim', $exValidTlds);
foreach($exValidTlds as $tld) {//if exist then
if(strstr($emailTld, ".".$tld)) {
if($tld == strrchr($emailTld, $tld)) {
return true;
}
}
}
return false;
}
}发布于 2015-03-14 05:34:21
function validateEmail($email) {
//validate email here from server side
if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
//validate tld
$validTlds = str_replace(".", "\.", VALID_EMAIL_TLDS);
$validTlds = "\.".str_replace(",", "|\.", $validTlds);
//$validTlds = str_replace(",", "|\.", $validTlds);
$emailArr = explode("@", $email);
$emailTld = $emailArr[1];
if ($emailTld == 'csiro.au')
{
//check main domain here
return true;
}
elseif (preg_match('/^[-a-z0-9]+('.$validTlds.')\z/', strtolower($emailTld))) {
//check main domain here
$exValidTlds = explode(",", VALID_EMAIL_TLDS);
$exValidTlds = array_map('trim', $exValidTlds);
foreach($exValidTlds as $tld) {//if exist then
if(strstr($emailTld, ".".$tld)) {
if($tld == strrchr($emailTld, $tld)) {
return true;
}
}
}
return false;
}
}
return false;
}
这个regexp对我非常有用:
.+@(?:(?:govt*)|(?:edu)|(?:ac)|(?:csiro))\.(?:au|nz)我使用这个工具来创建它:http://regexpal.com/
验证电子邮件是非常困难的:http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html
编辑:在重新阅读你的问题后,你似乎需要对含有子域名的电子邮件进行验证。这可能更适合于更开放的域:
.+@(?:\w+\.\w+)编辑2:所以问题是你的验证对于你的复杂性来说太轻了。
.+@(?:(?:.+\.(?:(?:govt*)|(?:edu)|(?:ac))\.(?:au|nz))|(?:csiro\.au))把它拆开:
.+ // Match at least 1 of any character
@ // An @ symbol
(?: // The group of everything right of the @ symbol
(?: // The group of domains that have subdomains
.+\. // At least one character in front of a .
(?: // govt, edu or ac
\. // a dot
(?: // au or nz
(?: // or simply 'csiro.au'您无法回避这样一个事实:您的四个域需要一个子域,而另一个不需要子域。
https://stackoverflow.com/questions/29045745
复制相似问题