3

I have a form that I want jquery to check and see if the input/select values with a class of required (These classes are in the #customerForm form) are blank. If they are I would like to prevent the form from submitting and change the color of the input/select box to red. If all of the input/select values with a class of required do have values, I would like to submit the form, fade the form inputs out and and fadeIn the next form. Here's my code:

$('#customerForm').submit(function (event) {
    var value = $('.required').each();
    if (value === "") {
        value.addClass("invalid");
        return false;
    } else {
        value.removeClass("invalid");
        $('#billForm, #shipForm').fadeOut();
        $('#payment-form').fadeIn();
    }
}); 

The problem is the form doesn't check for the input values and submits the form anyways. Any help would be amazing.

3 Answers 3

1

$('.required').each() returns an array of inputs.

You have to loop through each input and check the value before the form can be submit.

$( ".required" ).each(function() {
  //validation
});

more complete example:

$('#customerForm').submit(function (event) {
  var error = false;
  $( ".required" ).each(function() {
    if ($(this).val() === "") 
    {
      $(this).addClass("invalid");
      error = true;
    } 
    else 
    {
      $(this).removeClass("invalid");
    }
  });
  if(!error)
  {
    $('#billForm, #shipForm').fadeOut();
    $('#payment-form').fadeIn();
  }
  else
  {
    return false;
  }
}); 
Sign up to request clarification or add additional context in comments.

2 Comments

Strange. If all of the input fields are blank the class of invalid shows up. But if I enter one input value or all and press submit, The form submits and takes me to my form processing page (I don't want this).
Sorry, think it might have been the this which was supposed to be $(this) which was causing the issue, glad you got it working
0

You cant grab a field using each. Each is to iterate through them.

So you need to do this.

 $('#customerForm').submit(function (event) {
     var value = $('.required'); // Get all the required fields
     var cnt=0; // setup a counter
     // Loop therough required fields and apply error class based on value
     $.each(value, function(i,val){
           if (val.val() === "") {
             cnt++;//add to the count of invalid fields
             val.addClass("invalid");// apply error class
           } else {
             val.removeClass("invalid");
           }
     });
     // Check the cnt variable is larger than 0, means we have errors
     if(cnt!=0){
          return false;
     // otherwise, continue
     } else {
        $('#billForm, #shipForm').fadeOut();
        $('#payment-form').fadeIn();
     }
}); 

Comments

0

You should loop through each element like this

$('.required').each((function( index ) {
var error = 0;
value = $(this).html();
if (value === "") {
    this.addClass("invalid");
    error = 1;
} else {
    this.removeClass("invalid");
    $('#billForm, #shipForm').fadeOut();
    $('#payment-form').fadeIn();
}
if (error)
    return false;
});

Comments

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.