How to add dynamic rule to jQuery validator

"jQuery validator" is  really a very good plugin for validating your web forms on client side. Why I am recommending this plugin is because its flexibility and set of methods which offers complete customization.  Plugin offers lots of different methods so that you can customize it as per your requirement very easily. I have used it in many applications. There are many other jQuery based form validation tools which actually using this plugin as back bone of there plugins.

I was working on one application yesterday in which I need to load some form fields dynamically from AJAX call. In this case once input fields are populated in your form you may need to apply validation rule for these fields. This step is necessary because the original  $("#formId").validate method was initiated during the page load. So how to add dynamic rule to jQuery validator?? check the following solution.

If you want to add validation rule to form which is already loaded then its kind of tricky and you may need to use following function.

$("#synamicFieldId1").rules("add", {validateApp:['parameter1','parameter2']});

You can find the .rules() documentation with more examples here.


Post a Comment

Share this post with your friends