Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating multiple forms with same class using jquery validate

I have about 10 forms on one page with the same class. Each form should be able to validate and send individually. I am using the jquery validate plugin. I can't get it to work and all the forms are submitting the first one. Besides that I can't seem to target the errormessage div within a form with $(this).find('.error').html(error);

Each form looks like this:

<form method="post" class="alertform" action="">
<input type="text" onclick="clearText(this)" value="" class="required email" name="email">
<input type="hidden" value="1000004011320719" name="productid" class="productid">
<input type="submit" class="button" name="button" value="Set alert">
<div class="error">&nbsp;</div>
</form>

My JS:

$('.alertform').each( function(){
$(this).validate({
rules: {
        emailadres: {
            required: true,
            email: true
            }
    },
    messages: {

                    emailadres: {
            required: "Message 1",
            minlength: "Message 2",
            email: "Message 3"
            }

            },
          errorPlacement: function(error, element) {
 $(this).find('.error').html(error);
},
success: function(label) {
label.addClass("valid").text("")
},
submitHandler: function() { 

  var emailadres = $(this).find("input.email").val();
  var productid = $(this).find("input.productid").val();

  var dataString = 'emailadres=' + emailadres + '&productid=' + productid;

    $.ajax({
  type: "POST",
  url: "/setalert.php",
  data: dataString,
  success: function(msg) {

    if (msg==1) {
    $(this).find(".email").attr("value", ""); 

    $(this).find('.error').html("Pricealert set.")


    }
    else {


    $(this).find('.error').html("Oops.")

          }

}
});

}
})
});

Any help is appreciated!

like image 226
dacross Avatar asked Jun 08 '11 02:06

dacross


2 Answers

I had a look at your HTML n JS, can you try this?

$('.alertform').each( function(){
   var form = $(this);
   form.validate({

Basically change all your $(this) into form through out ur code

This is common mistake about $(this), sometime it lost its reference.

Hope helps

:)

like image 78
kororo Avatar answered Nov 09 '22 02:11

kororo


The complete working JS:

$('.alertform').each( function(){

var form = $(this);
form.validate({
rules: {
        emailadres: {
            required: true,
            email: true
            }
    },
    messages: {

                    emailadres: {
            required: "Required",
            minlength: "Fill it in",
            email: "Not valid"
            }

            },
          errorPlacement: function(error, element) {
          element.nextAll('div').html(error);

},
success: function(label) {
 label.addClass("valid").text("")
},

submitHandler: function() { 

  var email = form.find("input.email").val();
  var productid = form.find("input.productid").val();

  var dataString = 'email=' + email + '&productid=' + productid;

    $.ajax({
  type: "POST",
  url: "/myurl.php",
  data: dataString,
  success: function(msg) {

    if (msg==1) {
    form.find(".email").attr("value", ""); 

    form.find('.error').html("Thanks")


    }
    else {


    form.find('.error').html("Something went wrong")

          }

  }
 });


}
})
});
like image 22
dacross Avatar answered Nov 09 '22 04:11

dacross