【发布时间】:2012-07-18 20:36:14
【问题描述】:
我正在使用http://bassistance.de/jquery-plugins/jquery-plugin-validation/ 来验证我的表单。不幸的是,没有纯文本方法。所以我尝试自己写:
$(document).ready(function(){
$("#staffedit").validate(
{
rules: {
editDisplayName: {
textonly: true,
required: true
}
}}
);
jQuery.validator.addMethod(
"textonly",
function(value, element)
{console.log('textonly called.');
console.log(/[-.\w\s]/.test(value));
return this.optional(element) || /[-.\w\s]/.test(value);
},
jQuery.format("Please only enter letters, spaces, periods, or hyphens.")
);
});
该方法被调用,因为我的 console.log 函数会在适当的时间输出到控制台。我不明白的是,为什么我仍然可以在输入字段中输入 $ 或 *。我可以把任何东西放在那里,它仍然被评估为有效。
我使用http://regexpal.com/ 来测试正则表达式,它在那里运行良好。
那么,如何让插件只允许使用文本、空格、句点和连字符?
顺便说一句,我还希望允许使用带有重音符号的字符,例如 à、É、ā 或 ô...
【问题讨论】:
标签: regex jquery-plugins jquery-validate