开发者

datepicker JQuery Validations and Format

I am using the follo开发者_StackOverflowwing for datepicker in Jquery to format the date display in dd/mm/yy format and also I want the user not to select future date.Only one thing is working at a time.

<script type="text/javascript" language ="javascript" >
            $(function () {
                var date = new Date();
                var currentDate = date.getDate();
                var currentMonth = date.getMonth();
                var currentYear = date.getFullYear();
               $(".datepicker").datepicker({ dateFormat: 'dd/mm/yy' });
                $(".datepicker").datepicker({ maxDate: new Date(currentYear, currentMonth, currentDate) });

            });
</script>

How do make, both the dateformat and disable the future dates to work simultaneously. I am missing single bit, I don't know how to club this two validations or requirements togather.

Any answers Please?

Thank you.


I had a similar requirement for my code with a hire date. Here's how I did it:

$('#hireDate').datepicker({
    dateFormat: 'dd/mm/yy',
    maxDate: new Date(currentYear, currentMonth, currentDay)
});


The user can still enter a future date manually and weasel around your date picker. We had this problem on a production system.

If you want to additionally validate the date using the validation plugin, try this:

/**
 * Requires Datepicker and Validator
 *
 * Params: 
 * 0...dateformat. see datepicker
 * 1...date. Value "0" is "today"
 * 2...(optional). date to display. will be automatically filled if 0 and 1 are set.
 * usage:
 * myfield: { maxDate: ['m/d/yy', 0] }
 */
jQuery.validator.addMethod("maxDate", 
    function(value, element, params) {
        if (!params[0])
            throw 'params missing dateFormat';
        if (typeof(params[1]) == 'undefined' )
            throw 'params missing maxDate';

        var dateFormat = params[0];
        var maxDate = params[1];
        if (maxDate == 0) {
            maxDate = new Date();
            maxDate.setHours(0); // make it 00:00:0
            maxDate.setMinutes(0);
            maxDate.setSeconds(0);
            maxDate.setMilliseconds(0);
        }
        if (typeof(params[2]) == 'undefined' )
            params[2] = $.datepicker.formatDate(dateFormat, maxDate);

        try {
            var valueAsDate = $.datepicker.parseDate( dateFormat, value )
            return (valueAsDate < maxDate);
        } catch (x) {
            return false;
        }

    },'Must be greater than {2}.');


$("#myform").validate({
    rules: {
        datepicker : { 
            maxDate ['m/d/yy', 0]
        }
    }
});

HTML:

<input name="datepicker" class="datepicker" type="text"/>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜