64

How would I validate hidden inputs and not visible text inputs with jQuery Form Validation plugin? The problem is, that I'm using auto-suggest plugin, which generates a hidden input for selected items:

<input id="hiddenInput" type="hidden" name="something" value="1" />

I have 2 inputs like this (both of them only allow 1 item), which I want to validate and display the error in parent <td>. This is what I've gotten so far, but it doesn't display the error or submit a form, if the value is actually a number.

$("#form1").validate({
        rules: {
            something: {
                number:true,
                min:1,
                required:true
            }
        }
        })
Sparky
  • 98,165
  • 25
  • 199
  • 285
Gregor Menih
  • 5,036
  • 14
  • 44
  • 66

4 Answers4

125

To allow validation of hidden elements, override the ignore and set it to empty string:

$("#form1").validate({
    ignore: "",
    rules: {
        something: {
            number:true,
            min:1,
            required:true
        }
    }
});
Josh
  • 1,669
  • 1
  • 11
  • 15
  • Have you tried this? because this is giving an error when I use ignore : "". `Uncaught TypeError: Cannot read property 'settings' of undefined` – Jereme Jan 20 '17 at 06:33
  • 1
    By default, for the last several years, this plugin will ignore (not validate) all hidden fields. The proper format to tell the plugin to ignore nothing and validate everything is `ignore: []` – Sparky Jun 30 '19 at 13:53
30

You can use ignore option like this:

$("#form1").validate({
    ignore: "input[type='text']:hidden",
    rules: {
        something: {
            number:true,
            min:1,
            required:true
        }
    }
});

Default value of ignore option is :hidden which ignores all hidden fields and non-visible fields (display: none etc.)

Emre Erkan
  • 8,433
  • 3
  • 48
  • 53
15

If the other answers aren't working for you, try this instead. It removes all ignores for the form, so it would validate everything including hidden fields:

$.data($('form')[0], 'validator').settings.ignore = "";

To restore the settings to ignore hidden fields, use something like this:

$.data($('form')[0], 'validator').settings.ignore = "input[type='text']:hidden";

You can also use the above code to read back the current value.

CMcClymont
  • 586
  • 5
  • 6
  • I've tried it and it didn't work for me. It gives me `TypeError: $.data(...) is undefined`, I've checked `$` and it's proper jQuery object. Here is an alternative answer http://stackoverflow.com/a/8565769/1027198. – Michał Powaga Oct 09 '13 at 12:01
  • 5
    Oddly enough, your solution is the *only* one that worked for me - none of the other answers did. – Nathan Nov 21 '13 at 12:53
  • Confirmed working with Aspnet Core 3.1 MVC application – Andrew Grothe Apr 07 '20 at 14:42
5

Another way to validate hidden inputs is this:

$("#form1").validate({
        ignore: "not:hidden",
        rules: {
            something: {
                number:true,
                min:1,
                required:true
            }
        }
});
drneel
  • 2,887
  • 5
  • 30
  • 48
lopradi
  • 51
  • 1
  • 1