[jQuery] Re: Validate optional field

2007-05-14 Thread Oddish

Thanks a lot! :)

On May 14, 3:48 pm, "Dan G. Switzer, II" <[EMAIL PROTECTED]>
wrote:
> >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



[jQuery] Re: Validate optional field

2007-05-14 Thread Dan G. Switzer, II

>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