简体   繁体   English

验证电子邮件地址

[英]Validate an email address

How would I check that a user's email address ends in on of the three 我如何检查用户的电子邮件地址是否以三个中的一个结尾

@camel.com, @mel.com, @camelofegypt.com @ camel.com,@ mel.com,@ camelofegypt.com

This is my code so far, it just validates whether or not the user has provided any text 到目前为止,这是我的代码,它只是验证用户是否提供了任何文本

if ($.trim($("#email").val()).length === 0) {
alert('You must provide valid input');
return false;

I want to implement the email address validation into my code. 我想在我的代码中实现电子邮件地址验证。

use the following regex: 使用以下正则表达式:

var reg=/@(camel|mel|camelofegypt)\.com$/
 reg.test(email)

This only validates if you email ends in one of the three above. 仅当您的电子邮件以上述三个结尾之一结尾时,这才有效。 If you want ot know general email validation, search the web. 如果您想了解一般的电子邮件验证,请在网上搜索。 There are tons of those 有很多

As described in the library to regular expressions , it is difficult to truly validate an email address. 正则表达式库中所述,很难真正验证电子邮件地址。 However, the below taken from the above website will do a good job. 但是,以下摘自上述网站的内容将做得很好。

The official standard is known as RFC 2822. It describes the syntax that valid email addresses must adhere to. 官方标准称为RFC2822。它描述了有效电子邮件地址必须遵守的语法。 You can (but you shouldn't--read on) implement it with this regular expression: 您可以(但不应继续阅读)使用以下正则表达式实现它:

(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

Simple Regex: 简单的正则表达式:

\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b

Trade offs of validating email addresses: 权衡验证电子邮件地址:

Yes, there are a whole bunch of email addresses that my pet regex doesn't match. 是的,有一堆我的宠物正则表达式不匹配的电子邮件地址。 The most frequently quoted example are addresses on the .museum top level domain, which is longer than the 4 letters my regex allows for the top level domain. 最经常引用的示例是.museum顶级域上的地址,该地址比我的regex允许顶级域的4个字母长。 I accept this trade-off because the number of people using .museum email addresses is extremely low. 我接受这种折衷,因为使用.museum电子邮件地址的人数非常少。 I've never had a complaint that the order forms or newsletter subscription forms on the JGsoft websites refused a .museum address (which they would, since they use the above regex to validate the email address). 我从来没有抱怨过JGsoft网站上的订购表或新闻通讯订阅表拒绝了.museum地址(因为他们使用上述正则表达式来验证电子邮件地址,所以他们拒绝了)。

However, if you just want your specific domain this is definitely a possibility but it is not recommended to deny an email address because it fails these regular expressions. 但是,如果您只想使用自己的特定域,则绝对可以,但是不建议您拒绝电子邮件地址,因为它使这些正则表达式失败。

Taking the above you could simply validate using the following Regex: 考虑到以上内容,您可以使用以下正则表达式简单地进行验证:

\b[A-Z0-9._%+-]+@(camel|mel|camelofegypt)\.com\b

or: 要么:

^[A-Z0-9._%+-]+@(camel|mel|camelofegypt)\.com$

The difference between these two regex are simple, the first regex will match an email address contained within a longer string. 这两个正则表达式之间的区别很简单,第一个正则表达式将匹配包含在较长字符串中的电子邮件地址。 While the second regular expression will only match if the whole string is the email address. 而第二个正则表达式仅在整个字符串为电子邮件地址时才匹配。

JavaScript Regex: JavaScript正则表达式:

/\b[A-Z0-9._%+-]+@(camel|mel|camelofegypt)\.com\b/i

or: 要么:

/^[A-Z0-9._%+-]+@(camel|mel|camelofegypt)\.com$/i

Special Note: You should likely allow for case insensitive with your regex using the i parameter since John@CAMEL.com is the same as john@camel.com . 特别注意:由于John@CAMEL.comjohn@camel.com相同, john@camel.com您应该使用i参数允许对正则表达式不区分大小写。 Which i've done in the above regex. 我在上面的正则表达式中所做的。

function ValidateEmail(email) {
 var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  return regex.test(email);
}

OR 要么

This is a regular-expression email validation comparison that will test a bunch of valid/invalid email address against the regex provided by you in the textarea below. 这是一个正则表达式电子邮件验证比较,它将对照您在下面的文本区域中提供的正则表达式测试一堆有效/无效的电子邮件地址。

^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$

Try out this javascript 试试这个javascript

function IsEmail(email) {
 var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  return regex.test(email);
}

Try this function, 试试这个功能,

 function validateEmail($email) {
  var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
  if( !emailReg.test( $email ) ) {
   return false;
 } else {
  return true;
}
}


if( !validateEmail(email)) { /* do stuff here */ }
var x=$("#email").val();
var n=x.split("@");
if($.inArray(camel.com,n)!= -1)//checks if camel.com is there in your email address if not  the value will be -1 @is not given in front of camel.com because its split after @ which means that @will be present before it if camel.com is prescent.
  {
}                                   
else
if($.inArray(mel.com,n)!= -1)
{}
else
 if($.inArray(camelofegypth.com,n)!= -1)
{}
else
   alert("");

using new regex 使用新的正则表达式

demo Here 演示在这里

added support for Address tags (+ sign) 增加了对地址标签 (加号)的支持

function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
return pattern.test(emailAddress);
};

Note : Keep in mind that no 100% regex email check exists 注意: 请记住,不存在100%的正则表达式电子邮件检查

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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