>I'm trying to validate an optional field with the validation plugin,
>using a custom method. Here's the code:
>
>rules: {
>   birthdate: { validBirthdate:true }
>},
>messages: {
>    birthdate: {
>        validBirthdate: "Invalid birthdate"
>    }
>}
>
>How do I make this field optional? I only want to check the birthdate
>if the user has provided one. (I'm aware of the "date" method, but I'm
>purposely using a custom method).

This is going to be controlled by your custom function. If you look at
Jörn's built in validation methods, you'll see he validates the field if the
field is also required:

rangeLength: function(value, element, param) {
        var length = jQuery.validator.getLength(value, element);
        return !jQuery.validator.methods.required(value, element) || (
length >= param[0] && length <= param[1] );
},

You can use the same approach in your custom validation, or you can just
pass back "true" if the field is blank to say it passes validation.

-Dan

Reply via email to