2

If I have a required field on my form within my MVC3 page, and try submitting the form, the validation fires and the input control is given a light-red background color (fill) and it also shows a validation message. If I type in the input control, it will detect the value and remove both the coloring and validation method. This is how it should work.

In my case, I have mailing address fields, most of them required. I have an option on my site to select an address in a drop down list. When an address is selected by the user, all the address fields filled in using client side javascript. However, when I do this, my validation message and color aren't going away. So I need to somehow force the validation check.

How is this done?

Thanks in advance.

Jeff Reddy
  • 5,551
  • 9
  • 55
  • 88
  • possible duplicate of [How to manually trigger validation with jQuery validate?](http://stackoverflow.com/questions/1479255/how-to-manually-trigger-validation-with-jquery-validate) – Jason Berkan May 17 '13 at 17:02

1 Answers1

3

The reason validation fires for your inputs is because they hook up to your change, focus, blue, keypress, etc events. For a scenario of where you fill the fields in with script you'll need to specifically call each element that had it's value populated. The reason for this is because the change event will not be fired. Just select the element with jQuery and call the valid method on it for jQuery Validate. The valid function will force validation on the element when called.

jQuery Validate Valid Documentation

Example

$("#mySelect").change(function() {
    $("#myElement1").val("Some Value");
    $("#myElement1").valid();
});    
JustinMichaels
  • 1,092
  • 7
  • 12
  • 1
    Thanks, worked great for each textbox. But the Summary Validation area is still showing. Any idea how to clear that? Could be a second question. – Jeff Reddy May 17 '13 at 18:00