1

I have added a css class validation rule for jquery validator as follows:

jQuery.validator.addClassRules({
        requiredDatepicker: {
            required: true,
            date: true
        }
    });

However, when doing this I get the default error messages. How can I assign custom error messages when using addClassRules()?

2 Answers 2

2

See the Refactoring rules section on how to add custom messages.

1

You can't do this generally with the rule set, since class rules are just combinations of basic rules (for example even required is really required: { required: true } underneath) and those have the error messages. To get custom errors you'll have to set them per-element, or use the meta-data plugin to put the rules on the element directly.

4
  • How can I set the errors per element if I am using css classes as opposed to id?
    – devlife
    Commented Oct 30, 2010 at 17:48
  • 1
    @devlife - check out the metadata/validation demo here: jquery.bassistance.de/validate/demo/… it can be used with data attributes as well. Commented Oct 30, 2010 at 17:49
  • I must be missing something. Here is the html for my input: <input class="field datePicker arrivalDate requiredDatePicker departureDate {required:true, date:true, messages:{required:'enter a valid date', date:'enter a valid date'}}"type="text" value="">. However, it is not being validated...
    – devlife
    Commented Oct 30, 2010 at 18:25
  • apparently that's what I was missing =)
    – devlife
    Commented Oct 30, 2010 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.