繁体   English   中英

特定域的电子邮件验证

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM