CodeLover CodeLover - 1 year ago 49
jQuery Question

Required validation error show if i fill fields by jquery in mvc

I have a form with input elements. There are four elements in the form which are required. When i submit the form without it's selection, required error appears which is good. I fill these input fields by jquery on the selection of drop-down value. Now the problem is -> Suppose i submit the form without filling the input fields, the required validation error appears, now if i select the dropdown value and one of input fields auto populated as below:

//Drop down change event

//Fill my field
function fillFields(val){
$("#inputField1").val("autoPopulate"); //textbox fill with text "autoPopulate"

Now the problem is after auto populated, the required error still occur. On the other hand if i type in the text box manually, the required error disappears.
What's the problem?


By default, client side validation (jquery.validate.js) is triggered by the blur() event (or .keyup()) and just updating the value of a control using jquery does not trigger any events.

You can specifically trigger the event, for example


or you can call the .valid() method on the form after updating the values