且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery 验证插件:只接受字母字符?

更新时间:2023-02-21 12:21:45

如果您包含其他方法文件,这里是 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");