2

So I have been trying to do global override for all of the fields with a class 'required'. I got it so I can add a red background to any field that fails the validation with the class required like so:

$("#signup_form").validate({
    errorClass: "validateError"
});

I have tried using the errorPlacement: function as others have mentioned to remove the error param like so:

$("#signup_form").validate({
    errorClass: "validateError"
    errorPlacement: function (error, elemenet) {
       error.remove();
    },
});

This just kills the validation entirely.

One forum mentioned that I need to specify each field that the validation could expect and override the default 'required' message to be nothing in the messages: function. How can I set it globally seeing as this setup needs to be dynamic to anticipate any field with a class required set to it?

Thanks a lot

appthat
  • 816
  • 2
  • 10
  • 30
  • You are over-thinking this. By default, the plugin adds class `error` to any element with an error. It's that simple. Just target your CSS appropriately. – Sparky Aug 23 '13 at 22:50
  • That ended up being part of the solution. The other problem was the default message for 'required' I was trying to remove it globally but still use the error class for controlling background color. – appthat Aug 23 '13 at 23:36
  • See complete answer below. – Sparky Aug 24 '13 at 00:43

1 Answers1

4

To style elements that have validation errors, you would simply target the default class for errors with your CSS. The default class is error.

.error {
    background-color: #ff0000;
}

or perhaps more specifically:

input.error {
    background-color: #ff0000;
}

To globally change validation error messages for this plugin, use this:

jQuery.extend(jQuery.validator.messages, {
    required: "your message here"
});

Reference: https://stackoverflow.com/a/2457053/594235


To remove all error messages entirely, just place a return false; inside the errorPlacement callback function:

errorPlacement: function () {
    return false;
}

DEMO: http://jsfiddle.net/eP5HF/

Community
  • 1
  • 1
Sparky
  • 98,165
  • 25
  • 199
  • 285