jQuery 验证插件:只接受字母字符? [英] jQuery validation plugin: accept only alphabetical characters?
问题描述
我想使用 jQuery 的验证插件来验证一个只接受字母字符的字段,但似乎没有定义的规则.我用谷歌搜索过,但没有发现任何有用的东西.
I'd like to use jQuery's validation plugin to validate a field that only accepts alphabetical characters, but there doesn't seem to be a defined rule for it. I've searched google but I've found nothing useful.
有什么想法吗?
感谢您的帮助.
推荐答案
如果您包含其他方法文件,这里是 1.7 的当前文件:http://ajax.microsoft.com/ajax/jquery.validate/1.7/additional-methods.js
If you include the additional methods file, here's the current file for 1.7: http://ajax.microsoft.com/ajax/jquery.validate/1.7/additional-methods.js
您可以使用 lettersonly
规则:) 附加方法是您下载的 zip 的一部分,您总是可以找到最新的 这里.
You can use the lettersonly
rule :) The additional methods are part of the zip you download, you can always find the latest here.
这是一个例子:
$("form").validate({
rules: {
myField: { lettersonly: true }
}
});
值得注意的是,每个附加方法都是独立的,您可以包含特定的方法,只需将其放在您的 .validate()
调用之前:
It's worth noting, each additional method is independent, you can include that specific one, just place this before your .validate()
call:
jQuery.validator.addMethod("lettersonly", function(value, element) {
return this.optional(element) || /^[a-z]+$/i.test(value);
}, "Letters only please");
这篇关于jQuery 验证插件:只接受字母字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!