开发者

Turn Off MVC3 Attribute Validation Conditionally

开发者 https://www.devze.com 2023-03-30 09:02 出处:网络
I\'d like to create attribute validation for a check box or radio buttons but have that validation not be enforced when it is not applicable. Unfortunately, disabling the control or making it read-onl

I'd like to create attribute validation for a check box or radio buttons but have that validation not be enforced when it is not applicable. Unfortunately, disabling the control or making it read-only does not turn off the validation.

Does anyone know how from jQuery or JavaScr开发者_如何学Goipt, I could stop the validation when some state was true (the control was disabled or some other checkbox was unchecked, for example)?

Thanks, Paul


I believe this post will help.

How can I add, remove, or swap jQuery validation rules from a page?

To sum it up here's the code

var settings = $('form').validate().settings;
delete settings.rules.rightform_input1;
delete settings.messages.rightform_input1;

Setting would look something like this.

$('form').validate({
    rules: {
        leftform_input1: { required: true },
        leftform_input2: { required: true }
    },
    messages: {
        leftform_input1: "Field is required",
        leftform_input2: "Field is required"
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消