jQuery validator plugin 之 custom methods 案例1:multi email
1.add method
jQuery.validator.addMethod( "multiemail", function (value, element) { var email = value.split(/[;,]+/); // split element by , and ; valid = true; for (var i in email) { value = email[i]; valid = valid && jQuery.validator.methods.email.call(this, $.trim(value), element); } return valid; }, jQuery.validator.messages.multiemail );
2.validate事件绑定
$("form").validate({ debug: true, rules: { emailTest: { multiemail: true } }, messages: { emailTest: { multiemail: "You must enter a valid email, or comma separate multiple" } }, submitHandler: function(form) { return false; } });
3.调用
if (!$("Form").validate().form()) return false;
4.其他
相关html
<form> <input id='emailTest' name='emailTest' /> <input type='submit' /> </form>
相关js引用
<script src="/Scripts/jquery-2.1.3.js"></script> <script src="/Scripts/jquery.validate.js"></script>
C# 单个email的正则验证
Regex.IsMatch(eachMail, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$")
参考资料:
(multiple) emails in jQuery Validated text input
Javascript multiple email regexp validation
jQuery validation plugin multiple email addresses