Javascript 检查是否输入了正确的电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8398403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:50:57  来源:igfitidea点击:

Check if correct e-mail was entered

javascriptjqueryvalidation

提问by Ilja

I have a field for users to write their e-mail address in. It is optional, so I need to first check if something was entered in the field, if nothing was entered then do nothing, but if something was entered than check if it is an e-mail.

我有一个字段供用户输入他们的电子邮件地址。它是可选的,所以我需要先检查是否在该字段中输入了某些内容,如果没有输入则什么都不做,但如果输入了某些内容,则检查它是否是一个电子邮件。

Right now I'm at this point

现在我在这一点上

var email = $("input#sc_email").val();  
if (email == "") {                
    // If e-mail field is empty do nothing
} else {                          
     // If something was entered
    // [CODE HERE] Check if valid e-mail was entered, if not show error message
    $("label#email_error").show(); //error message
    $("input#sc_email").focus();   //focus on email field
    return false;  
} 

I'm not even sure if that is right, but I think it is. Right now I need help with gaps in code, I marked them as [CODE HERE]. Could anyone suggest a solution please.

我什至不确定这是否正确,但我认为是这样。现在我需要帮助解决代码中的空白,我将它们标记为[CODE HERE]. 任何人都可以提出解决方案。

回答by Rory McCrossan

You could use the jQuery validate pluginfor this, but if you only want to check the validity of an email address in one field, that is a little bit of overkill.

您可以为此使用jQuery 验证插件,但如果您只想检查一个字段中电子邮件地址的有效性,那就有点矫枉过正了。

Instead, the regular expression in the function below will make sure the format of the email address is correct, should a value have been entered.

相反,如果输入了值,下面函数中的正则表达式将确保电子邮件地址的格式正确。

var email = $("input#sc_email").val();  

if (email !== "") {  // If something was entered
    if (!isValidEmailAddress(email)) {
        $("label#email_error").show(); //error message
        $("input#sc_email").focus();   //focus on email field
        return false;  
    }
} 

function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^(("[\w-+\s]+")|([\w-+]+(?:\.[\w-+]+)*)|("[\w-+\s]+")([\w-+]+(?:\.[\w-+]+)*))(@((?:[\w-+]+\.)*\w[\w-+]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][\d]\.|1[\d]{2}\.|[\d]{1,2}\.))((25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{1,2})\.){2}(25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
};

回答by Connell

You could use regular expressions to validate the email address in JavaScript.

您可以使用正则表达式来验证 JavaScript 中的电子邮件地址。

function validateEmail(email) { 
    var re = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\
".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA
-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}

This will, however, only validate that it is a valid email address formatted string. [email protected]would be valid, even if no-one actually has that email address.

但是,这只会验证它是一个有效的电子邮件地址格式字符串。[email protected]将是有效的,即使实际上没有人拥有该电子邮件地址。

Have a look at this question.

看看这个问题

回答by Asghar

I would recommend validation both at client side and at server side, you could simply use this code:

我建议在客户端和服务器端进行验证,您可以简单地使用以下代码:

alert(/\S+@\S+\.\S+/.test('asdasd@[email protected]'));
alert(/\S+@\S+\.\S+/.test('[email protected]'));

but importantly, use server side validation and methodology, like sending click-link-mail, to verify your client email address or mailing random number etc.

但重要的是,使用服务器端验证和方法,例如发送点击链接邮件,来验证您的客户端电子邮件地址或邮寄随机数等。