Last active
September 28, 2015 13:08
-
-
Save myaumyau/1442915 to your computer and use it in GitHub Desktop.
[js]メールアドレス検証
rfc5321 || rfc5322 => MailAddressValidator.js
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Demo</title> | |
<style type="text/css"> | |
body { | |
margin: 0; | |
padding: 0; | |
} | |
p, div, ul, li, img { | |
margin: 0; | |
padding: 0; | |
} | |
ul { | |
list-style: none outside none; | |
} | |
textarea { | |
resize: none; | |
} | |
#container { | |
margin: 10px; | |
} | |
#result { | |
} | |
.notValid { | |
background-color: red; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="container"> | |
<input type="radio" value="0" id="rfc5321" name="rfc" checked="checked"><label for="rfc5321">rfc5321(Simple Mail Transfer Protocol)</label> | |
<input type="radio" value="0" id="rfc5322" name="rfc"><label for="rfc5322">rfc5322(Internet Message Format)</label> | |
<br /> | |
<input type="text" id="mailaddress" size="50"></input> | |
<input type="button" id="validate" value="validate" /> | |
</div> | |
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script> | |
<script type="text/javascript" src="https://gist.github.com/myaumyau/1442915/raw/MailAddressValidator.js"></script> | |
<script type="text/javascript"> | |
(function() { | |
var | |
onValidate = function() { | |
var r; | |
if ($('#rfc5321').is(':checked')) { | |
r = MailAddressValidator.validateRFC5321($('#mailaddress').val()); | |
} else { | |
r = MailAddressValidator.validateRFC5322($('#mailaddress').val()); | |
} | |
$('#mailaddress').toggleClass('notValid', !r); | |
}, | |
undef; | |
$().ready(function() { | |
$('#validate').click(onValidate); | |
}); | |
})(); | |
</script> | |
</body> | |
</html> |
This file contains hidden or 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
/* | |
* メールアドレスがRFC 5321, 5322にほぼ(*1)準拠しているかどうかを検証します。 | |
* *1 domain が IPアドレスは不可とか address-literal 非対応とか、一般的なメールアドレスを対象として削ってる | |
* | |
* [Reference] | |
* <http://tools.ietf.org/html/rfc5321> | |
* <http://tools.ietf.org/html/rfc5322> | |
* | |
* [Use] | |
* MailAddressValidator.validateRFC5321('[email protected]'); // false | |
* MailAddressValidator.validateRFC5322('[email protected]'); // false | |
* | |
*/ | |
(function(namespace, className) { | |
// local symbol | |
var _win = window, _doc = document; | |
var _ns = _win; | |
if (namespace) { | |
var nsArr = namespace.split('.'); | |
for (var i = 0, l = nsArr.length; i < l; i++) { | |
var ns = nsArr[i]; | |
if (typeof(_ns[ns]) == 'undefined') _ns[ns] = {}; | |
_ns = _ns[ns]; | |
} | |
} | |
if (!className) className = 'MailAddressValidator'; | |
var cls = _ns[className] = {}; | |
// RFC5321 | |
cls.rfc5321 = {}; | |
// Atom | |
cls.rfc5321.atext = "[A-Za-z0-9!#\\$%&'\\*\\+\\-/=\\?\\^_`\\{\\|}~]"; | |
cls.rfc5321.atom = cls.rfc5321.atext + '+'; | |
// Local-part | |
cls.rfc5321.dot_string = cls.rfc5321.atom + '(?:\\.' + cls.rfc5321.atom + ')*'; | |
cls.rfc5321.qtextSMTP = '[\\x20-\\x21\\x23-\\x5B\\x5D-\\x7E]'; | |
cls.rfc5321.quoted_pairSMTP = '\\\\[\\x20-\\x7E]'; | |
cls.rfc5321.Quoted_string = '\\"(?:' + cls.rfc5321.qtextSMTP + '|' + cls.rfc5321.quoted_pairSMTP + ')+\\"'; | |
cls.rfc5321.local_part = '(' + cls.rfc5321.dot_string + '|' + cls.rfc5321.Quoted_string + ')'; | |
// Domain | |
cls.rfc5321.letDig = '[A-Za-z0-9]'; | |
cls.rfc5321.ldhStr = '[A-Za-z0-9\\-]*' + cls.rfc5321.letDig + '+'; | |
cls.rfc5321.subdomain = cls.rfc5321.letDig + '(?:' + cls.rfc5321.ldhStr + ')*'; | |
cls.rfc5321.domain = '(' + cls.rfc5321.subdomain + '(?:\\.' + cls.rfc5321.subdomain + ')*)'; | |
// Mailbox(address-literal非対応) | |
cls.rfc5321.mailbox = cls.rfc5321.local_part + '@' + cls.rfc5321.domain; | |
// RFC5322 | |
cls.rfc5322 = {}; | |
// Quoted characters | |
cls.rfc5322.quoted_pair = '\\\\[\\x09\\x20-\\x7E]'; | |
// Quoted Strings | |
cls.rfc5322.qtext = '[\\x21\\x23-\\x5B\\x5D-\\x7E]'; | |
cls.rfc5322.qcontent = '(?:' + cls.rfc5322.qtext + '|' + cls.rfc5322.quoted_pair + ')'; | |
cls.rfc5322.quoted_string = '\\"(?:' + cls.rfc5322.qcontent + ')*\\"'; | |
// Atom | |
cls.rfc5322.atext = "[A-Za-z0-9!#\\$%&'\\*\\+\\-/=\\?\\^_`\\{\\|}~]"; | |
cls.rfc5322.atom = cls.rfc5322.atext + '+'; | |
cls.rfc5322.dot_atom = cls.rfc5322.atom + '(?:\\.' + cls.rfc5322.atom + ')*'; | |
// Addr-Spec | |
cls.rfc5322.domain = '(' + cls.rfc5322.dot_atom + ')'; | |
cls.rfc5322.local_part = '(' + cls.rfc5322.dot_atom + '|' + cls.rfc5322.quoted_string + ')'; | |
cls.rfc5322.addr_spec = cls.rfc5322.local_part + '@' + cls.rfc5322.domain; | |
cls.validateRFC5321 = function(address) { | |
var regExp = new RegExp('^' + cls.rfc5321.mailbox + '$'); | |
return regExp.test(address); | |
}; | |
cls.validateRFC5322 = function(address) { | |
var regExp = new RegExp('^' + cls.rfc5322.addr_spec + '$'); | |
return regExp.test(address); | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment