Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to add custom validation in validation.js

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 123
    Comment on it

    If you are using validation.js to add validation in your page and want to add some custom validation on the same, like I want a user can only enter alphabetic character with comma, dashes and space on first name text box. Please follow below code:

    <script src="YOUR_VALIDATION_FILE_PATH/js/validation.js"></script>
    $.validator.addMethod("loginRegex", function(value, element) {
            return this.optional(element) || /^[a-z\s\`\'\-]+$/i.test(value);
        }, "Username must contain only letters, spaces ` 'or dashes.");
    
    
    
    jQuery("#registration-frm").validate({                                        
    // Specify the validation rules
    rules : {
    user_fname : {
    required : true,
    loginRegex: true,  // You can see we have added our custom function here
    maxlength : 30
    }
    },
    messages : {
    user_fname : {
    required : "Please enter your first name",
    maxlength : "Only maximum {0} characters are required"
    }
    },
    submitHandler : function(form) {
    form.submit();
    }
    });
    
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: