jQuery validate plugin against custom jQueryUI datePickers

Posted by Enrique on Stack Overflow See other posts from Stack Overflow or by Enrique
Published on 2010-04-15T21:02:17Z Indexed on 2010/04/15 21:03 UTC
Read the original article Hit count: 372

I have some datePickers
I've customized these so they show only month and year
This is the code to create them

jQuery("input[name*='fechauso']").each(function() {
    jQuery(this).datepicker({
        changeMonth: true,
        changeYear: true,
        showButtonPanel: true,
        dateFormat: 'MM yy',
        constrainInput: true,
        showOn: 'button',
        buttonText: 'Seleccionar...',

        onClose: function(dateText, inst) { 
            var month = jQuery("#ui-datepicker-div .ui-datepicker-month :selected").val();
            var year = jQuery("#ui-datepicker-div .ui-datepicker-year :selected").val();
            jQuery(this).datepicker('setDate', new Date(year, month, 1));
        }
    });
});

Now I've added a custom validator method (using plugin) to check this:

  • If user didn't select a date using the button, field is empty, so the custom validator method should fire. This doesn't happen.

Here is the custom validate method

jQuery.validator.addMethod("isEmpty", function(value, element) {
    return (value == '');
}, "Must select a date with the button besides control");

jQuery("#ct_2_fechauso").rules("add", { 
    required: "#campotilde_psico:checked", 
    isEmpty: true
});

The problem is that even if I select a date, it always ask me to select a date again. datePicker fields should be readonly

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about jquery-validate