javascript - How can I force my MVC client side validation when updating values in script - Stack Overflow

If I have a required field on my form within my MVC3 page, and try submitting the form, the validation

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.

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.

Share Improve this question asked May 17, 2013 at 17:01 Jeff ReddyJeff Reddy 5,67010 gold badges58 silver badges91 bronze badges 1
  • possible duplicate of How to manually trigger validation with jQuery validate? – Jason Berkan Commented May 17, 2013 at 17:02
Add a ment  | 

1 Answer 1

Reset to default 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();
});    

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745400907a4626095.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信