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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2476378/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:35:28  来源:igfitidea点击:

jQuery validate plugin : accept letters only?

jqueryjquery-validate

提问by pixelboy

I'm using the validate plugin from http://bassistance.de/jquery-plugins/jquery-plugin-validation/

我正在使用http://bassistance.de/jquery-plugins/jquery-plugin-validation/ 中的验证插件

What i'm trying to find is a way to make some of my form fields accept letters only, no numbers, special chars etc...

我试图找到一种方法,让我的一些表单字段只接受字母,不接受数字,特殊字符等......

Any idea people ?Thanks a lot.

有什么想法吗?非常感谢。

回答by Marcos Placona

Simply add a custom validator, and use it like this:

只需添加一个自定义验证器,然后像这样使用它:

jQuery.validator.addMethod("accept", function(value, element, param) {
  return value.match(new RegExp("." + param + "$"));
});

Only numbers:

只有数字:

rules: {
  field: { accept: "[0-9]+" }
}

Only letters

只有字母

rules: {
  field: { accept: "[a-zA-Z]+" }
}

回答by basex

A small change.

一个小小的改变。

jQuery.validator.addMethod("accept", function(value, element, param) {
    return value.match(new RegExp("^" + param + "$"));
});

Because that way it was accepting expressions like "#abc".

因为那样它就接受了像“#abc”这样的表达。

回答by Orchid

Use below code in your script. this will add a new clause to your validator.

在脚本中使用以下代码。这将为您的验证器添加一个新子句。

$.validator.addMethod(
        "alphabetsOnly",
        function(value, element, regexp) {
            var re = new RegExp(regexp);
            return this.optional(element) || re.test(value);
        },
        "Please check your input values again!!!."
);
$("#formElement").rules("add", {alphabetsOnly: "^[a-zA-Z'.\s]$" })

回答by Jasim Juwel

Try This : ( This is perfect custom validation using jquery validator)

试试这个:(这是使用 jquery 验证器的完美自定义验证

$(function() {

    $.validator.addMethod("alphabetsnspace", function(value, element) {
        return this.optional(element) || /^[a-zA-Z ]*$/.test(value);
    });

    $("#add-employee").validate({
        rules: {
            employee_surname: {
                required: true,
                alphabetsnspace: true
            },
            employee_firstname: {
                required: true,
                alphabetsnspace: true
            },
            employee_othername: {
                alphabetsnspace: true
            },
            father_name: {
                required: true,
                alphabetsnspace: true
            },
            mother_name: {
                required: true,
                alphabetsnspace: true
            },
            spouse_name: {
                alphabetsnspace: true
            },
            ssn: {
                number: true,
            },
            phone_no: {
                number: true,
            },
            phone_no2: {
                number: true,
            },
            passport: {
                number: true,
            },
            driving_license: {
                number: true,
            },
            email: {
                email: true
            }
        },
        messages:
            {
                "employee_surname":{
                    alphabetsnspace: "Please Enter Only Letters"
                },
                "employee_firstname":{
                    alphabetsnspace: "Please Enter Only Letters"
                },
                "employee_othername":{
                    alphabetsnspace: "Please Enter Only Letters"
                },
                "father_name":{
                    alphabetsnspace: "Please Enter Only Letters"
                },
                "mother_name":{
                    alphabetsnspace: "Please Enter Only Letters"
                },
                "spouse_name":{
                    alphabetsnspace: "Please Enter Only Letters"
                },
            }
    });
});

回答by Nalan Madheswaran

Try like this:

像这样尝试:

        var numbers = /[0-9]+/;
        var SpCharacters = /^\s*[a-zA-Z0-9\s]+\s*$/;

        if (!numbers.test(name) && !SpCharacters.test(name)) {
            return [false, "Name should be alphabetical.", ""];
        }

回答by Bevin

The following method to add a custom validator works fine, But the validation happens on keyup, So the error keeps on popping up while the user is typing the text.

以下添加自定义验证器的方法工作正常,但验证发生在 keyup 上,因此在用户键入文本时错误不断弹出。

return value.match(new RegExp(""));
return value.match(new RegExp(""));

The below method works fine for Alphabets and Spaces, Ideal for name fields.

下面的方法适用于字母和空格,非常适合名称字段。

jQuery.validator.addMethod("alphabetsAndSpacesOnly", function (value, element) {
    return this.optional(element) || /^[a-zA-Z\s]+$/.test(value); });

    $("FieldId").rules("add", {
        alphabetsAndSpacesOnly: true,
        messages: { alphabetsAndSpacesOnly: "Please enter a valid name." }
    });
jQuery.validator.addMethod("alphabetsAndSpacesOnly", function (value, element) {
    return this.optional(element) || /^[a-zA-Z\s]+$/.test(value); });

    $("FieldId").rules("add", {
        alphabetsAndSpacesOnly: true,
        messages: { alphabetsAndSpacesOnly: "Please enter a valid name." }
    });

回答by Sparky

The jQuery Validate plugin already has a rule called lettersonlyas part of the additional-methods.jsfile, which looks like this...

jQuery的验证插件已经有一个名为规则lettersonly作为其一部分additional-methods.js文件,它看起来像这样...

$.validator.addMethod( "lettersonly", function( value, element ) {
    return this.optional( element ) || /^[a-z]+$/i.test( value );
}, "Letters only please" );

回答by Suresh Sakhare

Add cdn

添加cdn

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.15.0/additional-methods.js"></script>
rules:{
    txtname:{required: true,
            lettersonly:true
            }