jQuery 验证插件:只接受字母字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2794162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery validation plugin: accept only alphabetical characters?
提问by KeyStroke
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.
我想使用 jQuery 的验证插件来验证只接受字母字符的字段,但似乎没有定义的规则。我搜索过谷歌,但没有发现任何有用的东西。
Any ideas?
有任何想法吗?
Appreciate your help.
感谢你的帮助。
回答by Nick Craver
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
如果包含其他方法文件,这里是 1.7 的当前文件:http: //ajax.microsoft.com/ajax/jquery.validate/1.7/additional-methods.js
You can use the lettersonly
rule :) The additional methods are part of the zip you download, you can always find the latest here.
您可以使用lettersonly
规则 :) 附加方法是您下载的 zip 的一部分,您总是可以在这里找到最新的。
Here's an example:
下面是一个例子:
$("form").validate({
rules: {
myField: { lettersonly: true }
}
});
It's worth noting, each additional method is independent, you can include that specific one, just place this before your .validate()
call:
值得注意的是,每个附加方法都是独立的,您可以包含特定的方法,只需在.validate()
调用之前放置它:
jQuery.validator.addMethod("lettersonly", function(value, element) {
return this.optional(element) || /^[a-z]+$/i.test(value);
}, "Letters only please");
回答by adam
Be careful,
当心,
jQuery.validator.addMethod("lettersonly", function(value, element)
{
return this.optional(element) || /^[a-z," "]+$/i.test(value);
}, "Letters and spaces only please");
[a-z, " "] by adding the comma and quotation marks, you are allowing spaces, commas and quotation marks into the input box.
[az, " "] 通过添加逗号和引号,您允许在输入框中输入空格、逗号和引号。
For spaces + text, just do this:
对于空格 + 文本,只需执行以下操作:
jQuery.validator.addMethod("lettersonly", function(value, element)
{
return this.optional(element) || /^[a-z ]+$/i.test(value);
}, "Letters and spaces only please");
[a-z ] this allows spaces aswell as text only.
[az] 这仅允许空格和文本。
............................................................................
………………………………………………………………………………………………………………………………………………………… ......................................
also the message "Letters and spaces only please" is not required, if you already have a message in messages:
如果消息中已经有消息,则不需要“仅请使用字母和空格”消息:
messages:{
firstname:{
required: "Enter your first name",
minlength: jQuery.format("Enter at least (2) characters"),
maxlength:jQuery.format("First name too long more than (80) characters"),
lettersonly:jQuery.format("letters only mate")
},
Adam
亚当
回答by Steer360
Just a small addition to Nick's answer (which works great !) :
只是尼克答案的一个小补充(效果很好!):
If you want to allow spaces in between letters, for example , you may restrict to enter only letters in full name, but it should allow space as well - just list the space as below with a comma. And in the same way if you need to allow any other specific character:
例如,如果您想在字母之间允许空格,您可以限制只输入全名的字母,但它也应该允许空格 - 只需用逗号列出如下空格。如果您需要允许任何其他特定字符,以同样的方式:
jQuery.validator.addMethod("lettersonly", function(value, element)
{
return this.optional(element) || /^[a-z," "]+$/i.test(value);
}, "Letters and spaces only please");
回答by FlavioEscobar
Both Nick and Adam's answers work really well.
尼克和亚当的答案都非常有效。
I'd like to add a note if you want to allow latin characters like á
and ?
as I wanted to do:
我想,如果你想允许像拉丁字符添加注释á
和?
我想做的事:
jQuery.validator.addMethod('lettersonly', function(value, element) {
return this.optional(element) || /^[a-z á?a?àéê?èí??ìó???òú?üù??]+$/i.test(value);
}, "Letters and spaces only please");
回答by Sameera Prasad Jayasinghe
to allow letters ans spaces
允许字母和空格
jQuery.validator.addMethod("lettersonly", function(value, element) {
return this.optional(element) || /^[a-z\s]+$/i.test(value);
}, "Only alphabetical characters");
$('#yourform').validate({
rules: {
name_field: {
lettersonly: true
}
}
});
回答by Anup Shetty
$('.AlphabetsOnly').keypress(function (e) {
var regex = new RegExp(/^[a-zA-Z\s]+$/);
var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
if (regex.test(str)) {
return true;
}
else {
e.preventDefault();
return false;
}
});