PushALU
PushALU

Reputation: 277

Jquery validation, alert just once

If I run below, and both fields alder and gender is invalid, it'll alert two times. Is it possible to make it alert just once?

$(function(){
            $("#formTest").validate({           
                rules : {
                    alder : {
                        required: true
                    },
                    gender : {
                        required: true
                    }
                }, 

                errorPlacement: function (error, element) {
                    alert("Alert once");
                }
            });
});

Upvotes: 1

Views: 637

Answers (2)

Traveling Tech Guy
Traveling Tech Guy

Reputation: 27811

errorPlacement is called once for every error - it's not meant to alert a message, but to place it in the form. You can either try to use the groups feature, to group you fields together and get a single call for the entire group, or use the showErrors function to capture all errors and present them however you like:

$(".selector").validate({
  showErrors: function(errorMap, errorList) {
    alert(`Your form contains ${this.numberOfInvalids()} errors`);
  }
});

Upvotes: 5

nathan felix
nathan felix

Reputation: 396

Never used jQuery validate before but this might work:

$(function(){
  var checked = false;
  $("#formTest").validate({           
    rules : {
      alder : {
        required: true
      },
      gender : {
        required: true
      }
    }, 

    errorPlacement: function (error, element) {
      if (!checked) {
        alert("Alert once");
        checked = true;
      }
    }
  });
});

Upvotes: 0

Related Questions