sam sam - 2 months ago 9
jQuery Question

Adding custom error messages using Jquery Validation

I am trying to add the custom error messages for my field validation rather than using the default ones but it is not working after trying many different options. Below is my code snippet. Please help!

$('#myform').validate({ // initialize the plugin
submitHandler: function (form) {
console.log("Submitted!");
},
rules: {
accountNumber: {
required: true,
minlength: 8,
maxlength: 8,
min: 11111111,
digits: true

},
companyNumber: {
required: true,
digits: true

}
},

messages: {
min: 'Account number cannot be 00000000',
max: 'Account number cannot be 00000000',
digits: 'The account number must be a number'
}

});

});

Answer

Try this:

    $('#myform').validate({ // initialize the plugin
    submitHandler: function (form) {
        console.log("Submitted!");
   },
    rules: {
        accountNumber: {
            required: true,
            minlength: 8,
            maxlength: 8,
            min: 11111111,
            digits: true

            },
        companyNumber: {
            required: true,
            digits: true

        }
    },

   messages: {
   accountNumber: {
       min: 'Account number cannot be 00000000',
       max: 'Account number cannot be 00000000',
       digits: 'The account number must be a number'
   },
        companyNumber: {
        digits: 'The account number must be a number'
   }      
});});