最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I validate an email address from a specific domain? - Stack Overflow

programmeradmin0浏览0评论

I have this code to validate email input. My problem is, I want my form to accept only one specific domain. Such as, for example, [email protected]. I have no idea how to do this. Here's what I have so far:

function validateEmail(email)
{
    var splitted = email.match("^(.+)@(.+)$");
    if (splitted == null) return false;
    if (splitted[1] != null)
    {
        var regexp_user = /^\"?[\w-_\.]*\"?$/;
        if (splitted[1].match(regexp_user) == null) return false;
    }
    if (splitted[2] != null)
    {
        var regexp_domain = /^[\w-\.]*\.[A-Za-z]{2,4}$/;
        if (splitted[2].match(regexp_domain) == null)
        {
            var regexp_ip = /^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
            if (splitted[2].match(regexp_ip) == null) return false;
        } // if
        return true;
    }
    return false;
}

I have this code to validate email input. My problem is, I want my form to accept only one specific domain. Such as, for example, [email protected]. I have no idea how to do this. Here's what I have so far:

function validateEmail(email)
{
    var splitted = email.match("^(.+)@(.+)$");
    if (splitted == null) return false;
    if (splitted[1] != null)
    {
        var regexp_user = /^\"?[\w-_\.]*\"?$/;
        if (splitted[1].match(regexp_user) == null) return false;
    }
    if (splitted[2] != null)
    {
        var regexp_domain = /^[\w-\.]*\.[A-Za-z]{2,4}$/;
        if (splitted[2].match(regexp_domain) == null)
        {
            var regexp_ip = /^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
            if (splitted[2].match(regexp_ip) == null) return false;
        } // if
        return true;
    }
    return false;
}
Share Improve this question edited Dec 17, 2012 at 7:42 Jenna Pink asked Dec 17, 2012 at 5:41 Jenna PinkJenna Pink 611 gold badge2 silver badges4 bronze badges 2
  • 1 How about a simple, if( email.indexOf('@thisdomainonly') > 0) – Akhil Sekharan Commented Dec 17, 2012 at 5:50
  • If this is about security this is dangerous, as one could easily create a subdomain with @thisdomainonly..myrealdomain. – Jeroen van Dijk Commented Dec 16, 2015 at 9:40
Add a ment  | 

2 Answers 2

Reset to default 5

Modifying your function, you'd use something like this:

function validateEmail(email)
{
    var splitted = email.match("^(.+)@thisdomainonly\.$");
    if (splitted == null) return false;
    if (splitted[1] != null)
    {
        var regexp_user = /^\"?[\w-_\.]*\"?$/;
        if (splitted[1].match(regexp_user) == null) return false;
        return true;
    }
    return false;
}

But a more efficient version would be:

function validateEmail(email){
    return /^\"?[\w-_\.]*\"?@thisdomainonly\.$/.test(email);        
}

How about a simple indexOf checking:

        function checkEmail(email, domainName){
            if(email.indexOf(domainName) > 0)
                if((email.indexOf(domainName) + domainName.length) == email.length){
                    //do regex checking if any
                    return true;
                }

            alert('Please enter a valid email for : '+ domainName)
            return false;
        }
发布评论

评论列表(0)

  1. 暂无评论