style email address validation
I need a regular expression that will accept well-formed emails in several formats (see below) that will be input in a comma-separated list. I have the basic email address validation regex,
^[wd._%+-]+@(?:[wd-]+.)+(w{2,})(,|$)
which can handle test cases A and B , but not the others. I also tried
^(<)?[wd._%+-]+@(?:[wd-]+.)+(w{2,})(>)?(,|$)
which was able to handle A , B , and C , but only validated the first email address in each of test cases D and E . I haven't even gotten to testing a regex for format 3 .
tl;dr Need a regex that will validate email addresses 1 , 2 , and 3 .
Good website to test your regular expressions: Online Javascript Regex Tester
Data
Test Cases
A. nora@example.com
B. nora@example.com, fred@example.com
C. <nora@example.com>
, fred@example.com
D. <nora@example.com>, <fred@example.com>
E. fred@example.com, <nora@example.com>
Email Address Formats
1. xyz@example.com
2. <xyz@example.com>
3. "xyz" <xyz@example.com>
EDIT
I flagged this as a possible duplicate of:
Validate email address in JavaScript?
which, in turn, seems to be a duplicate of:
Using a regular expression to validate an email address
both of which contain much discussion on the validity of regex as email validation. However, the top-voted regexes provided don't seem to do quite what I want so I don't consider this answered yet.
It would be simpler to first split the comma-separated list into an array, and validate each member of the array individually. That would make the regex easier to write (and read and maintain), and also give you the ability to provide specific feedback to the user who entered the list ("the 3rd email address is invalid").
So assuming you did that with a split
var bits = csv.split(',');
Iterate through the bits
array
for (var i = 0; i < bits.length; ++i) {
if (!validateEmail(bits[i])) {
alert("Email #" + (i+1) + " is bogus");
}
}
Then for the regex, something like this will capture 2 and 3
("[a-z0-9s]+"s+)?<[wd._%+-]+@(?:[wd-]+.)+(w{2,})>
And you can use the simpler one to capture simple email addresses without the <
or the name in quotes in front of it.
A single regex will not necessarily run any faster than two if
tests, especially if you short-circuit the or
by putting the more likely one first. It's also harder to read and maintain. Lastly it's extra tricky because you need a lookahead: the final >
is only ok if the string ahead of the email address includes a <
right before the first character of the email.
So my $0.02 = not worth it. Just do two regexes.
This validateEmail function will check for the basic syntax of an email address ( xyz@example.com
). The included if
s will check for the alternate formatting ( <xyz@example.com>, 'xyz' <xyz@example.com>
) and only validate the actual email portion. Items with only < or > are deemed invalid for poor formatting ( Nope@example.com>
), same with any emails lacking the basic structure required ( invalidExample.com
).
var emailList = "abc@example.com,<lmn@example.com>,'xyz' <xyz@example.com>,invalidExample.com,Nope@example.com>,'Still93e-=48%5922=2 Good' <xyz@example.com>";
var emails = emailList.split(",");
//Loop through the array of emails
for (var i = 0; i < emails.length; i++) {
var isValid = 1;
var cur = emails[i];
// If it has a < or a >,
if( cur.indexOf("<") > -1 || cur.indexOf(">") > -1 ){
// Set it invalid
isValid = 0;
// But if it has both < and >
if( cur.indexOf("<") > -1 && cur.indexOf(">") > -1 ){
//Set it valid and set the cur email to the content between < and >
isValid = 1;
cur = cur.substr(cur.indexOf("<")+1, ( cur.indexOf(">") - cur.indexOf("<") - 1 ));
}
}
//Run the validate function
if ( !validateEmail(cur) )
isValid = 0;
// Output your results. valid = 1, not valid = 0
alert("Orig: "+emails[i] +"nStripped: "+cur+"nIs Valid: "+isValid);
}
function validateEmail(curEmail){
var emailValid = /.*@.*..*$/g;
return (curEmail.test(emailValid));
}
jsFiddle
None of the links or answers provided was the best answer for this question. Here's what solved it:
/*
* regex checks: must start with the beginning of a word or a left caret
* must end with either the end of a word or a right caret
* can handle example.example.com as possible domain
* email username can have + - _ and .
* not case sensitive
*/
var EMAIL_REGEX = /(<|^)[wd._%+-]+@(?:[wd-]+.)+(w{2,})(>|$)/i;
var emails = emailList.trim().split(',');
var validEmails = [];
var invalidEmails = [];
for (var i = 0; i < emails.length; i++) {
var current = emails[i].trim();
if(current !== "") {
//if something matching the regex can be found in the string
if(current.search(EMAIL_REGEX) !== -1) {
//check if it has either a front or back bracket
if(current.indexOf("<") > -1 || current.indexOf(">") > -1) {
//if it has both, find the email address in the string
if(current.indexOf("<") > -1 && current.indexOf(">") > -1) {
current = current.substr(current.indexOf("<")+1, current.indexOf(">")-current.indexOf("<") -1);
}
}
}
if(EMAIL_REGEX.test(current)) {
validEmails.push(current);
} else {
invalidEmails.push(current);
}
}
}
链接地址: http://www.djcxy.com/p/92594.html
上一篇: 电子邮件验证是错误的正则表达式
下一篇: 样式电子邮件地址验证