datepicker JQuery 验证和格式

发布于 2024-11-07 10:44:33 字数 685 浏览 0 评论 0原文

我在 Jquery 中使用以下日期选择器以 dd/mm/yy 格式格式化日期显示,而且我希望用户不要选择未来的日期。一次只有一件事在工作。

<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>

如何使日期格式和禁用未来日期同时工作。我缺少一点,我不知道如何将这两个验证或要求结合在一起。

请问有什么答案吗?

谢谢。

I am using the following 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心凉 2024-11-14 10:44:33

我对带有雇用日期的代码有类似的要求。我是这样做的:

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

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)
});
夜夜流光相皎洁 2024-11-14 10:44:33

用户仍然可以手动输入未来的日期并围绕您的日期选择器进行操作。我们在生产系统上遇到了这个问题。

如果您想使用验证插件另外验证日期,请尝试以下操作:

/**
 * 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"/>

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"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文