Adding a rule for jQuery validation plugin

I use the following rules to validate a username using the jQuery validation method. I want to add another rule in which the username should contain only an alphanumeric character and an underscore. How to add an additional method for this rule. Is it possible that if the user gave less than 4 characters, then I print a minimum length error message, and if the user gives invalid characters, then I give the wrong character error message? Thank.

$(document).ready(function() {
$("#sform").validate({
            rules: {
                username: {
                    required: true,
                    minlength: 4
                }
            },

            messages: {
                username: "Minimum length 4.",              
            }
        });
    });
+3
source share
4 answers

add as below

jQuery.validator.addMethod("alphanumeric", function(value, element) {
    return !jQuery.validator.methods.required(value, element) || /^[a-zA-Z0-9_]+$/i.test(value);
}
, "Letters, numbers or underscores only please"); 

and applicable below

$('validatorElement').validate({
    rules : {
        username : { alphanumeric : true }
    }
});
+5
source

Using remote ajax confirmation do this

$("#sform").validate({
    rules: {
    username: {
        required: true,
        minlength: 4,
        remote: 'alphanumertic.php'
        //check on server
    }
    },
    messages: {
    username: "Minimum length 4.",              
    }
});

, - ,

jQuery validate: ?

-

+1

. js, - :

http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/additional-methods.js

, :

$('validatorElement').validate({
    rules: {
        username: { alphanumeric : true }
    }
});
+1

, ,
:

  • , , : function mytest(value, element, params){...}

  • , , jQuery Validation. addMethod() validator.

:

$(document).ready(function() { 
  // other code
  // :  
  // :  
  $.validator.addMethod(
    "passwd",
     function(value, element, regexp) {
        var re = new RegExp(regexp);
           return this.optional(element) || re.test(value);
     },
    "Invalid input"
   );

  $("#add_user_form").validate({
      rules:{
        user_name: {
           required: true,
           minlength: 5,
           maxlength: 15,
           noSpace: true
        },
        password: {
          required: true,
          minlength: 8,
          passwd: "((?=(.*\\d.*){2,})(?=(.*[a-zA-Z].*){2,})(?=(.*[@#$(){}!~,.!^?/|+=-_%].*){2,}).{8,20})"
      }
    },

    messages:{
       user_name: {
         required: "*",
         minlength: " at least 5 characters",
         maxlenght: " only 15 characters",
         noSpace: " No space Please"
       },
       password: {
         required: "*",
         minlength: " Your password must be at least 8 characters long",
         passwd: " Invalid Password choice"
       }
 }); 
 //
 // more code in document.ready 

good links:
Starting with jQuery - How to write custom validation rules
How to add a rule to validate an expression?

0
source

Source: https://habr.com/ru/post/1796481/


All Articles