jQuery Validation Plugin允许电子邮件中包含特殊字符吗? [英] jQuery Validation Plugin allows special character in email?
问题描述
我使用的是 jQuery验证插件,对于电子邮件,一切都很好,因为ß 允许使用字符ist(当然应该是无效的)?
I'm using the jQuery Validation Plugin and everything works fine exept for emails, because there the ß character ist allowed (which of course should be non valid)?
这不仅发生在我的网站上,我什至可以绕过官方演示站点上的验证: http ://jquery.bassistance.de/validate/demo/
It's not something only happening at my site I can even bypass the validation on the officeial demo site: http://jquery.bassistance.de/validate/demo/
我正在使用的规则:
$("#form").validate({
rules: {
txtSubscribe: {
required: true,
email: true
}
}
});
是否可以在插件电子邮件验证规则中添加特定字符,或者有人知道如何实现适用于所有电子邮件规则的解决方案? (因为我经常在一个非常大的网站中使用它.)
Is there a way to add specific characters to the plugins email validation rule, or does anybody have a idea how to implement a solution that works for all email rules? (Because I'm using this quite often in a very large site.)
推荐答案
我在github https://github.com/jzaefferer/jquery-validation/issues/1012 ,并且似乎将在下一版本中使用HTML5电子邮件规范进行修复:
I opened a issue report on github https://github.com/jzaefferer/jquery-validation/issues/1012 and it seems like this is going to be fixed in the next release using the HTML5 specification for email: http://www.w3.org/TR/html5/forms.html#valid-e-mail-address
正则表达式:
/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
这是一个jsfiddle,显示在我的情况下这将返回正确的结果:
Here is a jsfiddle showing that this will return the correct result in my case:
也有可能覆盖默认验证:
It is aso possible to overwrite the default validation:
$.extend($.validator.methods, {
email: function(value, element) {
var re = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
return this.optional(element) || re.test(value);
}
});
这篇关于jQuery Validation Plugin允许电子邮件中包含特殊字符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!