13

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>
Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
redconservatory
  • 21,438
  • 40
  • 120
  • 189

4 Answers4

38

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.

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
Ryley
  • 21,046
  • 2
  • 67
  • 81
  • 1
    I tried this but then I'm getting this weird thing where a span is getting added (but not removed on the highlight)... – redconservatory Mar 30 '12 at 00:58
  • 2
    Ah you're right, the github version actually fixes this - check out the `errors` function here: https://github.com/jzaefferer/jquery-validation/blob/master/jquery.validate.js#L492 Replace your version of jquery Validate with that, or just that one function ought to be enough... – Ryley Mar 30 '12 at 15:32
0

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(,.

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
pdorgambide
  • 1,787
  • 19
  • 33
0

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");
    }
});
Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
Santiago
  • 2,190
  • 10
  • 30
  • 59
-1
$(".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?

Farside
  • 9,923
  • 4
  • 47
  • 60
joehand
  • 391
  • 2
  • 17