1

I'm using

   $(document).ready(function () {
    $(":radio:eq(1)").click(function () {
        $("#JointApplicantInfo").show(1000);
    });

    $(":radio:eq(0)").click(function () {
        $("#JointApplicantInfo").hide(1000);
    });

});

to hide a div but when hidden the validations from

src="~/Scripts/jquery.validate.min.js">

src="~/Scripts/jquery.validate.unobtrusive.min.js">

are still happening even when hidden.

I am using [required] tags in the model to cause validation to happen.

I am unsure of how to make this not happen. I understand that it requires more scripting, but I am a novice and cannot figure it out.

2
  • Validation is independent of how a value is display, therefore it will always be validated if created as an input element. Based on that, there is definitely a code smell of: why do you want a hidden element on the page that also has validation? Commented May 1, 2014 at 4:50
  • 1
    If it is not a mandatory field (hence you allow it to be hidden) then you should not decorate it with [required] annotation. Commented May 1, 2014 at 15:32

1 Answer 1

2

You can override the jQuery validation and tell it to ignore hidden fields:

$(function() {
    var settngs = $.data($('form')[0], 'validator').settings;
    settings.ignore = ":not(:visible)";
});

Reference

Edit: I should add though, as others have commented, it is not really correct to annotate a field as [Required] if it is only required under certain circumstances.

Sign up to request clarification or add additional context in comments.

2 Comments

I don't necessarily have to put [required] in the model, but essentially when someone clicks "yes" the joint part of the app opens up and i want those fields to be validated when it's shown.
If you have the [Required] attribute, the server side validation will fail when you use ModelState.IsValid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.