Last active
July 10, 2020 07:17
-
-
Save CatTail/4191834 to your computer and use it in GitHub Desktop.
Javascript: validate email address
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Using regular expression to validate email address. | |
* Exactly email address format refer to http://en.wikipedia.org/wiki/Email_address. | |
* @author [email protected] | |
*/ | |
var validateEmail = (function() { | |
// ATTENSION: escape is really mess because you have to escape in string and | |
// in regular expression. | |
var normal = "0-9a-zA-Z\\!#\\$%&'\\*\\+\\-\\/\\=\\?\\^_`\\{\\|\\}~"; | |
// mix contain normal character and special character | |
// special character \ " need to be escaped | |
var mix = '\\(\\),:;<>@\\[\\](\\\\\\\\)(\\\\")0-9a-zA-Z\\!#\\$%&\'\\*\\+-\\/\\=\\?\\^_`\\{\\|\\}~\\.\\s'; | |
// local part | |
var mixPattern = '"['+mix+']*"'; | |
var normalPattern = '[' + normal + '("")]+?'; | |
var localPattern = ['^((', normalPattern, '\\.)*', normalPattern, ')'].join(''); | |
// domain part | |
var hostnamePattern = '(:?[0-9a-zA-Z\\-]+\\.)*[0-9a-zA-Z\\-]+'; | |
var ipPattern = '\\[.+?\\]'; // TODO: handle IPv4 and IPv6 | |
var domainPattern = ['(?:(?:', hostnamePattern, ')|(?:', ipPattern, '))$'].join(''); | |
var commentPattern = "(?:\\(.*?\\))?"; | |
var pattern = localPattern + '@' + domainPattern; | |
var mixreg = new RegExp(mixPattern, 'g'); | |
var reg = new RegExp(pattern, 'g'); | |
return function(email) { | |
var valid = true; | |
// reset regular expression | |
reg.lastIndex = 0; | |
// TODO: I want to combine special pattern into normal pattern. | |
// Which means just one regular expression can handle everything. | |
// Anybody have any good idea please contact with me([email protected]) | |
email = email.replace(mixreg, '""'); | |
return reg.test(email); | |
}; | |
}()); | |
(function() { | |
var validEmails = [ | |
'[email protected]', | |
'[email protected]', | |
'[email protected]', | |
'[email protected]', | |
'user@[IPv6:2001:db8:1ff::a0b:dbd0]', | |
'"much.more unusual"@example.com', | |
'"[email protected]"@example.com', | |
'"very.(),:;<>[]\\".VERY.\\"very@\\\\ \\"very\\".unusual"@strange.example.com', | |
'0@a', | |
'postbox@com', | |
'!#$%&\'*+-/=?^_`{}|[email protected]', | |
'"()<>[]:,;@\\\\\\"!#$%&\'*+-/=?^_{}| ~ ? ^_{}|~.a"@example.org', | |
'""@example.org' | |
]; | |
var invalidEmails = [ | |
'Abc.example.com', | |
'[email protected]', | |
'[email protected]', | |
'A@b@[email protected]', | |
'a"b(c)d,e:f;g<h>i[j\\k][email protected]', | |
// 'just"not"[email protected]', | |
'this is"not\\[email protected]', | |
'this\\ still\\"not\\\\[email protected]' | |
]; | |
var i, len; | |
console.log('===valid email==='); | |
for (i=0, len=validEmails.length;i<len;i++) { | |
console.log(validateEmail(validEmails[i])); | |
} | |
console.log('===invalid email==='); | |
for (i=0, len=invalidEmails.length;i<len;i++) { | |
console.log(validateEmail(invalidEmails[i])); | |
} | |
// Output: | |
// ===valid email=== | |
// true | |
// true | |
// true | |
// true | |
// true | |
// true | |
// true | |
// true | |
// true | |
// true | |
// true | |
// true | |
// true | |
// ===invalid email=== | |
// false | |
// false | |
// false | |
// false | |
// false | |
// false | |
// false | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how usse for one input