acctman
acctman

Reputation: 4349

jQuery.validate display error in a error message box

i'm using the follow below to display error message next to each unfilled field. I'd like to simplify things and just have a ErrorBox appear that says "Please fill in all fields" when the submit button is clicked and fields are not filled. class="required" is being used on each field type that is required. how would I do this, do I use showError ?

<script type="text/javascript">
$(document).ready(function() {
    $("#form1").validate({
        errorLabelContainer: $("#form1 div.error")
    });
</script>

Upvotes: 1

Views: 6102

Answers (2)

codeandcloud
codeandcloud

Reputation: 55288

The validate plugin has invalidHandler: function(e, validator) which will suit your needs.

A small demo: http://jsfiddle.net/codovations/5YHQb/1/

Upvotes: 0

Sora
Sora

Reputation: 2551

Code

Displays a message above the form, indicating how many fields are invalid when the user tries to submit an invalid form.

$(".selector").validate({
invalidHandler: function(form, validator) {
  var errors = validator.numberOfInvalids();
  if (errors) {
    var message = errors == 1
      ? 'You missed 1 field. It has been highlighted'
      : 'You missed ' + errors + ' fields. They have been highlighted';
    $("div.error span").html(message);
    $("div.error").show();
  } else {
    $("div.error").hide();
  }
}
 })

instead of using a div use another jquery library that can display a pop up window with the same error message

Upvotes: 2

Related Questions