redconservatory
redconservatory

Reputation: 21924

jQuery validate add class to error element?

I have the following code using jQuery Validate.

$("#register").validate({
    debug: true,
    errorClass:'error',
    validClass:'success',
    errorElement:'span',
    highlight: function (element, errorClass, validClass) { 
        $(element).parents("div.control-group")
                  .addClass(errorClass)
                  .removeClass(validClass);
    }, 
    unhighlight: function (element, errorClass, validClass) {
        $(element).parents(".error")
                  .removeClass(errorClass)
                  .addClass(validClass); 
    }
});

Is there a way to add a class to the errorElement, 'span' so that it's...:

<span class="help-inline error">Message</span>

Upvotes: 13

Views: 53949

Answers (4)

Santiago
Santiago

Reputation: 2320

Because I think this is for Twitter Booststrap, this is a better solution with no need to update jquery validation:

$("#register").validate({
    debug: true,
    errorClass: 'help-inline',
    validClass: 'success',
    errorElement: 'span',
    highlight: function(element, errorClass, validClass) {
      $(element).parents("div.control-group").addClass("error");

    },
    unhighlight: function(element, errorClass, validClass) {
      $(element).parents(".error").removeClass("error");
    }
});

Upvotes: 0

joehand
joehand

Reputation: 391

$(".error").addClass('help-inline')

will add the help-inline class to all DOM elements with the error class already.

Is that what you are looking for?

Upvotes: -1

pdorgambide
pdorgambide

Reputation: 1867

An alternative of Ryley answer that not include the 'help-inline' into 'control-group' that avoid input offset on highlight.

$("#register").validate({
    debug: true,
    errorClass: 'error help-inline',
    validClass: 'success',
    errorElement: 'span',
    highlight: function(element, errorClass, validClass) {
      $(element).parents("div.control-group").addClass('error').removeClass('success');
    },
    unhighlight: function(element, errorClass, validClass) {
      $(element).parents(".error").removeClass('error').addClass('success');
    }
});

I cound't to include that issue like a comment of the answer...;o(,.

Upvotes: 0

Ryley
Ryley

Reputation: 21226

When you specify the errorClass, there's nothing stopping you from making it two things: "help-inline error", i.e.:

$("#register").validate({
    debug: true,
    errorClass: 'error help-inline',
    validClass: 'success',
    errorElement: 'span',
    highlight: function(element, errorClass, validClass) {
      $(element).parents("div.control-group").addClass(errorClass).removeClass(validClass);
    },
    unhighlight: function(element, errorClass, validClass) {
      $(element).parents(".error").removeClass(errorClass).addClass(validClass);
    }
});

Note that you have to use version 1.10 of jQuery Validate to support this. Earlier versions only allow one errorClass.

Upvotes: 38

Related Questions