views:

60

answers:

2

Hi there,

I've been using the jQuery form validator, but I can't seem to figure out how to trigger it by class. Take this example:

$("#myform").validate({
  rules: {
    field: {
      required: true,
      date: true
    }
  }
});

Where field it is expecting the name of the particular input. But, what if I have the following:

<input class="date".... />

How can I tell the validator to validate all inputs with a "date" class as a date?

A: 

Just supplying the input with a class that matches one of the validation methods is sufficient to have the plugin validate that input using the method. If this isn't working for you, then I'd use Firefox/Firebug to check to make sure that you don't have some javascript errors that are causing the plugin not to run.

tvanfosson
+1  A: 
jQuery.validator.addClassRules({
    date: {
        required: true,
        date: true,
    },
})
Claudio Redi
Beautiful, thanks!
joshcomley