使用 javascript 进行超级简单的电子邮件验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4964691/
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-23 14:59:12  来源:igfitidea点击:

Super simple email validation with javascript

javascriptjquery

提问by willdanceforfun

I'm making a really simple email validation script that basically just checks the following

我正在制作一个非常简单的电子邮件验证脚本,它基本上只检查以下内容

  1. that the email isn't blank
  2. the the email contains an @ symbol with at least 1 character before it
  3. that there is a domain ie @ with at least 2 letters after it
  4. that it ends with a fullstop with at least 2 letters after it
  1. 电子邮件不是空白的
  2. 电子邮件包含一个 @ 符号,前面至少有 1 个字符
  3. 有一个域 ie @ 后面至少有 2 个字母
  4. 它以句号结尾,后面至少有 2 个字母

I know there are many more checks, but I look at these regex rules and my mind stops working. I figure if I started with something small like this I might be able to wrap my brain around more complex rules.

我知道还有更多的检查,但我看着这些正则表达式规则,我的思绪停止了工作。我想如果我从像这样的小东西开始,我可能能够将我的大脑围绕在更复杂的规则上。

Currently using some jquery I do the following:

目前使用一些 jquery 我执行以下操作:

 var booking_email = $('input[name=booking_email]').val();

 if(booking_email == '' || booking_email.indexOf('@') == -1 || booking_email.indexOf('.') == -1) {

   // perform my alert

 }

This is enough to stop 90% of bogus emails so far... I would just like to make it a bit more effective because currently my rule will allow emails like '@domain.com' or 'user@domain.' because it only checks that there is a fullstop and an @ symbol.

到目前为止,这足以阻止 90% 的虚假电子邮件......我只是想让它更有效一点,因为目前我的规则将允许像“@domain.com”或“user@domain”这样的电子邮件。因为它只检查是否有句号和@ 符号。

Thanks for any tips.

感谢您提供任何提示。

回答by Ashley Williams

What others have suggested should work fine, but if you want to keep things simple, try this:

其他人的建议应该可以正常工作,但是如果您想保持简单,请尝试以下操作:

var booking_email = $('input[name=booking_email]').val();

if( /(.+)@(.+){2,}\.(.+){2,}/.test(booking_email) ){
  // valid email
} else {
  // invalid email
}

Even if you decide to go with something more robust, it should help you understand how simple regex can be at times. :)

即使您决定使用更强大的东西,它也应该可以帮助您了解正则表达式有时是多么简单。:)

回答by gonchuki

The least possible greedy validation you an do is with this RegExp /^.+@.+\..+$/
It will only ensure that the address fits within the most basic requirements you mentioned: a character before the @ and something before and after the dot in the domain part. Validating more than that will probably be wrong (you always have the chance of blacklisting a valid email).

您要做的最不可能的贪婪验证是使用这个 RegExp/^.+@.+\..+$/
它只会确保地址符合您提到的最基本要求:@ 之前的字符以及域部分中点之前和之后的某些内容。验证更多可能是错误的(您总是有机会将有效电子邮件列入黑名单)。

use it like this:

像这样使用它:

var is_valid_email = function(email) { return /^.+@.+\..+$/.test(email); }

回答by iivel

RegexLib.com ( http://regexlib.com/Search.aspx?k=email) has hundreds of email validation routines for a reason. I'd recommend you read this article: http://www.unwrongest.com/blog/email-validation-regular-expressions/and if you decide to continue using regex for validation, my favorite testing utility is the free regex designer available for free here: http://www.radsoftware.com.au/regexdesigner/... test all emails in a LARGE list (available for free download or purchase ... or use your own current DB) to ensure your regex is acceptable within your constraints.

RegexLib.com ( http://regexlib.com/Search.aspx?k=email) 有数百个电子邮件验证例程是有原因的。我建议您阅读这篇文章:http: //www.unwrongest.com/blog/email-validation-regular-expressions/如果您决定继续使用正则表达式进行验证,我最喜欢的测试实用程序是可用的免费正则表达式设计器在此处免费:http: //www.radsoftware.com.au/regexdesigner/...测试大列表中的所有电子邮件(可免费下载或购买...或使用您自己当前的数据库)以确保您的正则表达式在你的限制范围内可以接受。

I would recommend a basic test (many at the top of regexlib.com ... I'm not taking credit for the work of theirs I use routinely), followed by a email validation routine that requires user interaction. It is the only real way to 'validate' an email address.

我会推荐一个基本的测试(很多在 regexlib.com 的顶部......我不认为我经常使用他们的工作),然后是需要用户交互的电子邮件验证例程。这是“验证”电子邮件地址的唯一真正方法。

回答by Abhishek Sengupta

If you want to check only for Business Emails then try this :

如果您只想检查企业电子邮件,请尝试以下操作:

<script type="text/javascript">
$(document).ready(function(e){
     $('#sendReq').click(function(){ 
     var email = $('#emailAddress').val();
     var reg = /^([\w-\.]+@(?!gmail.com)(?!yahoo.com)(?!hotmail.com)(?!yahoo.co.in)(?!aol.com)(?!abc.com)(?!xyz.com)(?!pqr.com)(?!rediffmail.com)(?!live.com)(?!outlook.com)(?!me.com)(?!msn.com)(?!ymail.com)([\w-]+\.)+[\w-]{2,4})?$/;
      if (reg.test(email)){
     return 0;
     }
     else{
     alert('Please Enter Business Email Address');
     return false;
     }
     });
    });
</script>

回答by Maxim Zaslavsky

Try:

尝试:

function valid_email(email) {
   return email.match(/^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i);
}

That is the best available email validation regex, according to this article. I recommend using this, unless your goal is something really simple but not fully compatible.

根据这篇文章,这是最好的电子邮件验证正则表达式。我建议使用它,除非您的目标非常简单但不完全兼容。

回答by geedew

Emails are very difficult to test, but you can have a somewhat restrictive, but simple email validation. I use this one myself, which does a pretty decent job of making sure it's 95% an email address.

电子邮件很难测试,但您可以进行一些限制性但简单的电子邮件验证。我自己使用这个,它在确保 95% 是电子邮件地址方面做得相当不错。

var simpleValidEmail = function( email ) {
    return email.length < 256 && /^[^@]+@[^@]{2,}\.[^@]{2,}$/.test(email);
};

I would recommend this for a 'simple' validation.

我建议将其用于“简单”验证。

To see why this is a hard thing to solve, see: How to validate an email address using a regular expression?

要了解为什么这很难解决,请参阅:如何使用正则表达式验证电子邮件地址?

Update: corrected regular expression

更新:更正正则表达式

回答by Ian Christian Myers

You can try this:

你可以试试这个:

var booking_email = $('input[name=booking_email]').val();
if(booking_email.match(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/)) {
  // valid email
} else {
  // not valid
}