我正在尝试验证具有电子邮件地址的表单,其地址设置为' true'如图所示:
$( "#myform" ).validate({
rules: {
field: {
required: true,
email: true
}
}
});
我查看了jquery插件http://jqueryvalidation.org/email-method/,我发现只有在' @'之后才能验证电子邮件地址。符号(以上链接中的示例)。我制作了一个电子邮件验证脚本,基本上只是检查完整的电子邮件地址,包括' dot'在末尾。任何想法如何实现这个??
答案 0 :(得分:3)
您可以在正则表达式中添加jQuery.validator.addMethod,请尝试以下操作: -
jQuery.validator.addMethod("emailfull", function(value, element) {
return this.optional(element) || /^([a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+(\.[a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+)*|"((([ \t]*\r\n)?[ \t]+)?([\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*(([ \t]*\r\n)?[ \t]+)?")@(([a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.)+([a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.?$/i.test(value);
}, "Please enter valid email address!");
然后将其用作: -
$( "#myform" ).validate({
rules: {
field: {
required: true,
emailfull: true
}
}
});