Feeds:
Posts
Comments

Archive for the ‘JQuery’ Category

I had problems adding a input field dynamically to the JQuery client side validation. This is how I solved the problem:

In javascript, I added the following, when checking a checkbox:

$("#idOfMyInputField").attr("data-val", true);
$("#idOfMyInputField").attr("data-val-required", "Custom error message");

I also needed to re-initialize the validation of the form (this does the trick to the validator to find the new validation):

var formSelector = "#idOfMyForm";
$(formSelector)
  .removeData("validator")
  .removeData("unobtrusiveValidation");
$.validator
  .unobtrusive
  .parse(formSelector);

When, un-checking the  checkbox, I simply removed the data-val attributes with:

$("#idOfMyInputField)".removeAttr("data-val");
$("#idOfMyInputField").removeAttr("data-val-required");

And then, re-run the re-initialize code once again (like above) so the validation is removed for the input field.

I also tried using the rules add method directly on the input field i JQuery, but somehow I didn’t get it to work (maybe the version of JQuery we use is too old for this, I know there are some bugs at least for the messages part that is fixed).

Read Full Post »