【发布时间】:2011-11-17 16:53:35
【问题描述】:
我需要使用插件 jQuery Validate 验证文本区域,为此我使用正则表达式并向插件添加方法:
$.validator.addMethod(
"regex",
function(value, element, regexp) {
var check = false;
var re = new RegExp(regexp);
return this.optional(element) || re.test(value);
},
"No special Characters allowed here. Use only upper and lowercase letters (A through Z; a through z), numbers and punctuation marks (. , : ; ? ' ' \" - = ~ ! @ # $ % ^ & * ( ) _ + / < > { } )"
);
然后在选项中添加正则表达式:
comments:{
required: true,
maxlength: 8000,
regex: /[^A-Za-z\d\-\=\~\!@#\%&\*\(\)_\+\\\/<>\?\{\}\.\$‘\^\+\"\';:,\s]/
}
这以某种方式“起作用”,它确实检测到无效字符并显示消息,问题是它仅在特殊字符是框中唯一的情况下起作用,例如:
| `` ° ¬ // This shows the error message but...
test | // This won't show the message
因此,如果存在一个允许的字符,则验证将停止工作。我错过了什么吗?
附:我很确定这与插件有关,因为我只用 javascript 测试了正则表达式,它运行良好。
【问题讨论】:
标签: jquery regex jquery-plugins jquery-validate