这是jquery验证示例中的自定义方法,如何在电子邮件中允许多个域
$.validator.addMethod("baseEmail", function(value, element) {
return this.optional(element) || /^[a-zA-Z0-9ąćęłńóśżźĄĆĘŁŃÓŚŻŹ._-]+@(yahoo\.com|gmail\.com|hotmail\.com)$/i.test(value);
}, "Only this domains are allowed (gmail, yahoo, hotmail)");
html代码
<form action="" name="form" id="form">
<div class="field">
<input type="email" name="email" id="email" class="required">
</div>
<div class="field">
<input type="submit" value="Validate!">
</div>
jquery代码
$("#form").validate({
highlight: function(element) {
$(element).closest('.field').removeClass('success').addClass('errorBlock');
},
success: function(element) {
$(element).closest('.field').removeClass('errorBlock').addClass('success');
},
rules: {
email : {
required: true,
email: true,
baseEmail: true }
},
messages:{
email:{
required: "required",
email: "email is required",
}
}
});
});
因此,您可以通过此代码验证电子邮件地址并添加多域支持。