Roy Barber
Roy Barber

Reputation: 312

add class to submit button if form is valid

Im using the jQuery validate plugin to validate a form, im wanting to add a class "next" to the submit button if the form is valid only

Send

This is the code i have at the moment all working and validating, just need a function to add the class if valid: http://jsfiddle.net/kKSjL/

Link to jQuery validation page: http://docs.jquery.com/Plugins/Validation/

Upvotes: 0

Views: 3767

Answers (2)

Ryley
Ryley

Reputation: 21226

Archer has it generally, you just need to specify that as a submitHandler within the validate call:

$("#commentForm").validate({
    submitHandler:function(form){
        if (this.valid()){
          $('#submit').addClass('next');
          form.submit();
        } 
    }  
});

Here it is in action: http://jsfiddle.net/ryleyb/kKSjL/1/

Upvotes: 1

Reinstate Monica Cellio
Reinstate Monica Cellio

Reputation: 26163

However you're triggering the validation check, you need this code...

if ($("#commentForm").valid()) {
    $("#submit").addClass("next");
} else {
    $("#submit").removeClass("next");
}

Upvotes: 0

Related Questions