jQuery datepicker和自定义验证 [英] jquery datepicker and custom validation

查看:87
本文介绍了jQuery datepicker和自定义验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要添加自定义验证(我认为)以验证来自用户的输入.这是我的用例: 我正在使用jQuery ui datepicker选择日期,本地化是这样的:

I need to add custom validation(I think) for validating input from an user. Here's my use case: I'm using the jquery ui datepicker for selecting dates, with localization like this:

$.datepicker.setDefaults( $.datepicker.regional[ currentLocale ] );

我使用了低音强度验证插件,并且对date:true使用了规则,并且这样的规则不适用于不同的格式(或者如果适用,请告诉我!).因此,我制作了自己的日期验证方法,例如

I use the bassistance validation plugin, and using rules for date:true and such does not work with different formats(or if they do please tell me how!). So I've made my own date validation methods like this

   $.validator.addMethod("validDate", function(value) {
        return parseDateString(value) != null;
    }, jQuery.validator.messages.date);

除了在日期选择器中选择一个日期这一事实外,所有其他方法都非常有效,在更改组件值之前会触发验证!所以我实际上验证了先前的值....

This all works very well except for the fact when selecting a date in the datepicker the validation is fired before the value of the componet is changed! So I actually validate the previous value....

有人对我有解决方案吗? 在此先感谢:)

Does anyone have a solution for me? Thanks in advance:)

推荐答案

您可以触发验证在用户关闭 datepicker弹出窗口:

You could trigger validation upon the user closing the datepicker popup:

$("#birthdate").datepicker({
    onClose: function() {
        /* Validate a specific element: */
        $("form").validate().element("#birthdate");
    }
});

使用validate element() 函数将启用您可以立即验证特定字段.

Using validate's element() function will enable you to validate a particular field immediately.

我创建了一个示例此处,其中任何年份为2011年的日期都将触发失败验证.

I've created an example here in which any date who's year != 2011 will trigger failed validation.

这篇关于jQuery datepicker和自定义验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆