2

I have the jQuery Validation plugin:

//name validation
cnForm.validate({
    rules: {
        customerName: {
            required: true,
            minlength: 3
        },
        date: {
            required: true,
            dateRegex: /^(0[1-9]|1[012])[- \/.](0[1-9]|[12][0-9]|3[01])[- \/.](19|20)\d\d$/,
            maxlength: 10
        }               
    },
    onkeyup: false,
    onclick: false,
    onfocusout: false,
    messages: {
        customerName: {
            required: "Unable to proceed, search field is empty"
        },
        date: {
            required: "Unable to proceed, search field is empty"
        }   
    },
    errorPlacement: function(error, element) {
        var trigger = element.next('.ui-datepicker-trigger');
        error.insertAfter(trigger.length > 0 ? trigger : element);
    }
});

Whenever I input data that causes an error, I get the appropriate new label to display after the input box/button, but the class error is also applied to the input box. Is there a way to avoid assigning that class to the box? Thanks.

Sparky
  • 98,165
  • 25
  • 199
  • 285
Joe Essey
  • 3,457
  • 8
  • 40
  • 69

3 Answers3

6

You can use highlight call back to override the default behavior.

$(".selector").validate({
    highlight: function(element, errorClass) {
        // Override the default behavior here
    }
});
aarryy
  • 502
  • 5
  • 20
  • nice, I just added that and put a `//do nothing` comment in the method. – Joe Essey Aug 07 '13 at 15:45
  • @JoeEssey, that's not a very good way to do it. At least put a `return false` in there. – Sparky Aug 07 '13 at 15:55
  • Thanks for the recommendation @aarryy. If you have a moment can you please explain why? Is it so there is never some type error thrown when a function is called? – Joe Essey Aug 07 '13 at 15:58
  • @JoeEssey, it's a matter of proper programming practices/habits... why would you want an empty function? Maybe this time it works... maybe next time it chokes a future revision of the plugin. – Sparky Aug 07 '13 at 16:03
  • Hi JoeEssey, I supposed you were asking @Sparky. I actually use the same way if I just want to suppress the default behavior so I'm curious as well – aarryy Aug 07 '13 at 16:04
  • The `return` ensures the function will stop. See: http://www.quirksmode.org/js/function.html – Sparky Aug 07 '13 at 16:09
6

"...but the class error is also applied to the input box. Is there a way to avoid assigning that class to the box?"

Use the highlight and unhighlight callback functions to override the default functions.

These are the defaults...

    highlight: function (element, errorClass, validClass) {
        if (element.attr('type') === "radio") {
            this.findByName(element.attr('name')).addClass(errorClass).removeClass(validClass);
        } else {
            $(element).addClass(errorClass).removeClass(validClass);
        }
    },
    unhighlight: function (element, errorClass, validClass) {
        if (element.attr('type') === "radio") {
            this.findByName(element.attr('name')).removeClass(errorClass).addClass(validClass);
        } else {
            $(element).removeClass(errorClass).addClass(validClass);
        }
    }

As per docs...

The callback gets passed three arguments:

  • element
    Type: Element
    The invalid DOM element, usually an input.

  • errorClass
    Type: String
    Current value of the errorClass option.

  • validClass
    Type: String
    Current value of the validClass option.


EDIT:

To entirely remove this functionality...

$('#myform').validate({
    highlight: function (element, errorClass, validClass) {
        return false;  // ensure this function stops
    },
    unhighlight: function (element, errorClass, validClass) {
        return false;  // ensure this function stops
    },
    // other options
});
Sparky
  • 98,165
  • 25
  • 199
  • 285
1

"...but the class error is also applied to the input box. Is there a way to avoid assigning that class to the box?"

$('#formId').validate({
        errorElement: 'span',
        errorClass: 'errorMessage',
        errorPlacement: function (error, element) {
            error.insertAfter(element);
        },
        highlight: function (element, errorClass, validClass) {
            $(element).removeClass(errorClass);
        },
        ...
});
Alexander
  • 1,152
  • 1
  • 16
  • 18