jQuery验证插件:仅接受字母字符吗? [英] jQuery validation plugin: accept only alphabetical characters?

查看:71
本文介绍了jQuery验证插件:仅接受字母字符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用jQuery的验证插件来验证仅接受字母字符的字段,但似乎没有为其定义的规则.我已经搜索过Google,但没有发现任何有用的信息.

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的当前文件:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆