Ways to show conditional error messages in jquery validate plugin
Posted By : Shiv Kumar | 30-Jun-2016
Sometimes we are required to show validation messages depending upon some conditions and check. Jquery Validate plugin provides different ways as described below to do the same.
Way 1 : Using "depends". It is not a rule but the property which toggles the rules on or off. "depends" is used only in rules section not in messages section.
{ eventName: { required: { depends: function(ele) { return $('#eventCheckBox').is(':checked'); } }, minlength: 3 }, ... }, messages: { eventName: { required: "Event name is required", minlength: "Event name should be atleast 3 characters long." }, .... },
Way 2 : Using Inline check inside the rule section :
{ eventName: { required: $('#eventCheckBox').is(':checked')? true : false, minlength: 3 } }, ... messages: { username: { required: "Event name is required", minlength: "Event name should be atleast 3 characters long." } ... },
Way 3 : conditional checks in addMethod:
jQuery.validator.addMethod("isValidMemberType", function(value, element) { var order = $(element).attr('data-order'); if(value == 'PEOPLE_AND_COMPANIES') return true; else if(value == 'PEOPLE_ONLY' && $('#company'+order).val() != 0) return false; else if(value == 'COMPANIES_ONLY' && $('#user'+order).val() != 0) return false; else return true; }, function(value,element){ if($(element).val() == 'PEOPLE_ONLY') return $L("people.only.setting.error"); else if($(element).val() == 'COMPANIES_ONLY') return $L("company.only.setting.error"); });
Thanks
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Shiv Kumar
Shiv is an experienced Java Developer with a strong background in multiple technologies. He specializes in defining system architectures to ensure reliable and resilient solutions. He possesses a comprehensive understanding of the latest technologies and has hands-on experience in Core Java, Spring Boot, Hibernate, Apache Kafka messaging queue, Redis, as well as relational databases like MySQL and PostgreSQL, and non-relational databases like MongoDB. He excels in API implementations, Microservices, Web Services development, testing, and deployments. Shiv actively contributes to code enhancements and consistently delivers valuable contributions to various client projects, including Fabtrack, Pando, Pandojo, Digikam, WhatsApp Integration, Croniz, Punchin Application, Script TV, Bhaasha, and more. He demonstrates strong analytical skills and a creative mindset. In addition, he has a passion for reading books and exploring new technologies and innovations.