开发者

jQuery validate plugin against custom jQueryUI datePickers

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:

开发者_如何学Python
  • 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


try to use this

$.datepicker.setDefaults({
    closeOnSelect: true,
    onSelect: function () {
        if (typeof $(this).valid == 'function') {
            $(this).valid();
        }
    }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜