2

I have function to validation some special forms but I'm not able to exclude hidden elements

function specialFormValidation() {

    $("#specialForm").validate({
        ignore: ':hidden',
        errorPlacement: function(sError, oElement) {

            $('.tooltip ').remove();

            $(oElement).tooltip({
                title: sError[0].textContent + $(oElement).is(':hidden'),
                animation: false,
                placement: 'right',
                trigger: 'manual',
                container: 'body'
            });         

            $('.error').tooltip('show');
        }

    });

Like you can see I have set ignore parameter but is still showing tooltip for hidden elements, I'm using jQuery Validatre v1.10.0 and bootstrap tooltip. I can exclude visible elements if I put ignore: ':visible', but its not working for hidden elements.

Probably the ignore element not working on elements which was hidden by script after DOM was loaded

I try to fix it like that (to refresh visibility status) but is also not working:

 function specialFormValidation() {
    var ignored = $(':hidden');

    $("#specialForm").validate({
        ignore: ignored,
        errorPlacement: function(sError, oElement) {
        ...
Sparky
  • 98,165
  • 25
  • 199
  • 285
LJ Wadowski
  • 6,424
  • 11
  • 43
  • 76

2 Answers2

1

The ignore option is for telling the plugin which input elements to ignore (for validation). I'm not sure why you need to worry about your tooltips since these are not input elements, and they would never be targeted for validation by this plugin in the first place.


Ignoring all hidden elements is the default behavior. In fact ':hidden' is the same as what the plugin is already using.

In other words, as long as you're using version 1.9+ of the plugin, you don't need to specify ignore: ':hidden' at all. Just leave out the ignore option entirely and the plugin will ignore anything that's hidden.

Quote OP:

"Probably the ignore element not working on elements which was hidden by script after DOM was loaded"

This is not how it works. If you dynamically toggle the visibility of an element, the plugin will only ignore it while it's hidden.

"I try to fix it like that (to refresh visibility status) but is also not working:"

You cannot call .validate() multiple times (with or without new options). The .validate() method is only called once on page load to initialize the plugin. Any subsequent calls are always ignored.

Sparky
  • 98,165
  • 25
  • 199
  • 285
-1

You have to call "ignore" on document ready before you actually validate your form

$(document).ready(function () {
   $.validator.setDefaults({
       ignore: []
   });
});