如何验证特定网域的电子邮件地址? [英] How can I validate an email address from a specific domain?

查看:160
本文介绍了如何验证特定网域的电子邮件地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码验证电子邮件输入。我的问题是,我希望我的表单只接受一个特定的域。例如,test@thisdomainonly.com。我不知道该怎么做这是我到目前为止:

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, test@thisdomainonly.com. 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;
}


推荐答案

修改功能, d使用这样的东西:

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

function validateEmail(email)
{
    var splitted = email.match("^(.+)@thisdomainonly\.com$");
    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;
}

但是更有效的版本是:

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

这篇关于如何验证特定网域的电子邮件地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆