I am using jQuery unobtrusive validation on a form. Some fields are hidden, and some of these hidden fields require validation and others do not. I understand in order to validate hidden fields I can do this:

var validator = $("form").data("validator");
validator.settings.ignore = "";

But this then validates all hidden fields. How do I specify which hidden fields should be validated?

有帮助吗?

解决方案

You can add a class(like validate) to those hidden fields which has to be validated then

validator.settings.ignore = ":hidden:not(.validate)";

其他提示

Put a class on the fields to ignore, such as validator-ignore, then set that selector in the property:

validator.settings.ignore = ".validator-ignore";

Ok. Here is what worked for me

$.validator.setDefaults({
            ignore: ":hidden:not(.validate)"
        });
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top