jQuery validate plugin : accept letters only?

后端 未结 8 1373
被撕碎了的回忆
被撕碎了的回忆 2020-11-29 07:23

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

What i\'m trying to find is a way to make some of my form fields a

相关标签:
8条回答
  • 2020-11-29 07:55

    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.", ""];
            }
    
    0 讨论(0)
  • 2020-11-29 07:58

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

    $(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"
                    },
                }
        });
    });
    
    0 讨论(0)
  • 2020-11-29 08:01

    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.

    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." }
        });
    
    0 讨论(0)
  • 2020-11-29 08:08

    The jQuery Validate plugin already has a rule called lettersonly as part of the additional-methods.js file, which looks like this...

    $.validator.addMethod( "lettersonly", function( value, element ) {
        return this.optional( element ) || /^[a-z]+$/i.test( value );
    }, "Letters only please" );
    
    0 讨论(0)
  • 2020-11-29 08:09

    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]+" }
    }
    
    0 讨论(0)
  • 2020-11-29 08:09

    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".

    0 讨论(0)
提交回复
热议问题