Question

i have a problem with jquery validate (v. 1.11.1) that is driving me insane... I have the following contact form (i'm using bootstrap):

<form class="form-horizontal" id="contact-form" name="contact-form" role="form" method="post" enctype="multipart/form-data" action="">
<fieldset>

<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10 controls">
<input type="text" class="form-control" id="name" name="name" placeholder="Name">
</div>
</div>

<div class="form-group">
  <label for="email" class="col-sm-2 control-label">Email</label>
    <div class="col-sm-10 controls">
       <input type="email" class="form-control" id="email" name="email" placeholder="Email">
     </div>
</div>

<div class="form-group">
 <label for="message" class="col-sm-2 control-label">Message</label>
  <div class="col-sm-10 controls">
   <textarea class="form-control" rows="7" id="message" name="message"></textarea>
  </div>
 </div>

<div class="form-group">
  <div class="col-sm-offset-2 col-sm-10">
   <button type="submit" class="btn btn-success btn-lg pull-right">Send</button>
   </div>
</div>

</fieldset>
</form>

This is my validation code:

$(document).ready(function(){
   $('#contact-form').validate(
      {
       rules: {
           name: {
                  minlength: 2,
                  required: true
                 },
          email: {
                  required: true,
                  email: true
                 },
         message: {
                   minlength: 2,
                   required: true
                 }
            },
        errorPlacement: function(error, element) {},
       highlight: function(element) {
       $(element).closest('.form-group').removeClass('has-success').addClass('has-error');
        },
      success: function(element) {
        $(element).closest('.form-group').removeClass('has-error').addClass('has-success');
           }
      });

  });

The problem is that the success function doesn't add the "has-success" class. The success function is fired (i tried putting an alert() in it). The error class is being added correctly.

I have tried to specify the success class with: validClass: "has-success" but this adds the class to the input, while i need to add it to the parent .form-group

Any help is appreciated.

Thanks

Was it helpful?

Solution

You need to use the function unhighlight, instead of success.

 unhighlight: function (element) {
                  $(element)
                      .closest('.form-group')
                      .removeClass('has-error')
                      .addClass('has-success');

              }

Check out this fiddle: http://jsfiddle.net/mL5Vq/

Also, remember to close your input tags

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top