当更新脚本中的值时,如何强制MVC客户端验证?

时间:2021-07-19 16:19:03

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.

如果在我的MVC3页面中有一个需要的字段,并尝试提交表单,验证触发和输入控件将得到一个亮红色的背景颜色(填充),它还显示一个验证消息。如果我输入控件,它将检测值并删除着色和验证方法。这就是它的运作方式。

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.

在我的例子中,我有邮寄地址字段,其中大部分是必需的。我在我的网站上有一个选择,在下拉列表中选择一个地址。当用户选择一个地址时,所有使用客户端javascript的地址字段。然而,当我这样做时,我的验证信息和颜色不会消失。所以我需要强制验证检查。

How is this done?

这是怎么做的?

Thanks in advance.

提前谢谢。

1 个解决方案

#1


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选择元素,并调用它的有效方法jQuery Validate。有效的函数将在调用时强制对元素进行验证。

jQuery Validate Valid Documentation

jQuery验证有效证件

Example

例子

$("#mySelect").change(function() {
    $("#myElement1").val("Some Value");
    $("#myElement1").valid();
});    

#1


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选择元素,并调用它的有效方法jQuery Validate。有效的函数将在调用时强制对元素进行验证。

jQuery Validate Valid Documentation

jQuery验证有效证件

Example

例子

$("#mySelect").change(function() {
    $("#myElement1").val("Some Value");
    $("#myElement1").valid();
});