[英]Email validation for specific domains
我需要電子郵件驗證方面的幫助,此驗證代碼以特定格式(例如test @ test.gov.au,test @ something.ac.au)編碼所有電子郵件,我希望將其以test@something.au格式使用
(注意:這里只允許我進入gov.au,edu.au,govt.nz,ac.au和csiro.au的5個域)
我的代碼如下
JS:
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;
}
}
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; }
這個正則表達式對我來說非常有效:
.+@(?:(?: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'
您無法避免以下事實:您的四個域需要一個子域,而一個不需要。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.