5

I am using the JQuery Validation plugin to handle form validation.

The problem I am having is that when a form is hidden, the validation plugin ignores the fields that need to be validated and jumps straight to submitting the form.

Example: http://jsfiddle.net/Qg5WQ/

I have had a look through the plugin's options and googled this issue but I cannot find anything that specifies why fields within a hidden form are ignored.

It is worth noting that if there are two forms on a page, where one is hidden and one is displayed, that both forms are successfully validated if both forms use the same validation method. However, if you call two seperate validation methods then the fields within the hidden form are still ignored.

Validating two forms using the same validation function: http://jsfiddle.net/Qg5WQ/1/

Validating two forms using different validation functions: http://jsfiddle.net/Qg5WQ/2/

Does anyone know if this is a bug or is there a specific way to validate hidden forms?

1
  • i am facing the same issues, so i am making my own code to validate the form. (when i am in .aspx webform) Commented Dec 15, 2011 at 11:47

2 Answers 2

11

Try this http://jsfiddle.net/Qg5WQ/3/

I added the ignore : hidden option to the validate call.

$("form").validate({
  ignore: 'hidden',
  rules: {
    "first": "required"
  },
  invalidHandler: function() {
    alert("error");   
  },
  submitHandler: function( form ) {
    alert("no error");
  }
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that oddly does work. I seem to be able to add anything into the ignore field and it works. I guess this must force it to iterate through the fields which makes me think that this must be a bug. Thanks for the help!
@MyHeadHurts - Yes, I noticed that, very strange, but good it works anyway=)
Just a quick note that the reason this works is because you're missing the colon from the front of :hidden. You're essentially telling it to ignore any html <hidden> tags that may exist rather than telling it to ignore inputs of type hidden.
7

If you check out the ignore option on the documentation page, it says that the default value is :hidden. This obviously means that if you don't specify the ignore option explicitly, the validation will ignore hidden inputs.

Simply define the ignore option (it can be left blank) and it should fix the problem.

$("form").validate({
  ignore: '',
  rules: {"hidden_field": {required:true}}
});

1 Comment

+1 - thanks, that explains why adding anything into ignore still worked

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.