覆盖jQuery验证插件电子邮件地址验证
我发现jQuery验证插件regex不足以满足我的要求.它接受任何电子邮件地址xxx@hotmail.x作为有效的电子邮件地址,而我希望能够提供此正则表达式/^([a-zA-Z0-9_.-+])+@@(([[a-zA -Z0-9-])+.)+([[a-zA-Z0-9] {2,4})+ $/,以便验证地址的完整.com部分.我更关心的是能够提供自己的正则表达式而不是简单的正则表达式(因为没有用于电子邮件验证的简单正则表达式)
I find that jQuery validation plugin regex to be insufficient for my requirement. It accepts any email address xxx@hotmail.x as a valid email address whereas I want to be able to supply this regex /^([a-zA-Z0-9_.-+])+\@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/ so that it would validate complete .com part of the address. I'm more concerned about being able to supply my own regex than getting a fool proof regex(as there is no fool proof regex for email validation)
仅供参考:我也在做服务器端验证,但是现在我不担心哪个正则表达式是正确的电子邮件地址.
Just FYI: I'm also doing server side validation but at this point I'm not worried about which email address regex is right.
在jQuery validate plugin的"rules"部分中有没有办法做到这一点?
Is there a way to do that within the "rules" section of jQuery validate plugin?
这是我现在的规则部分:
This is my rules section right now:
rules: {
email: {
required: {
depends:function(){
$(this).val($.trim($(this).val()));
return true;
}
},
email: true
},
我不会这样做,但是为了得到答案,您需要添加自己的自定义验证.
I wouldn't do this but for the sake of an answer you need to add your own custom validation.
//custom validation rule
$.validator.addMethod("customemail",
function(value, element) {
return /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(value);
},
"Sorry, I've enabled very strict email validation"
);
然后在您的规则中添加:
Then to your rules add:
rules: {
email: {
required: {
depends:function(){
$(this).val($.trim($(this).val()));
return true;
}
},
customemail: true
},