使用jquery验证来验证内联jquery datepicker [英] Validate an inline jquery Datepicker using jquery validation

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

问题描述

我试图确保使用我的嵌入式日期选择器选择了一个日期.嵌入式日期选择器提供了一个隐藏的输入框,因此很难做到这一点.为了尝试显示该消息,我使用invalidHandler重新定位了错误消息.

I am trying to ensure that a date has been selected using my inline datepicker.The inline datepicker feeds a hidden input box, so it's tough to require that. To try to get the message to display, I have relocated my error messages using invalidHandler.

我的代码:

<script type="text/javascript">
      jQuery('#date').datepicker({
        inline: true,   
        beforeShowDay: checkDayOfWeek,
        onSelect: function(dateText, inst) { $("#ship_date").val(dateText); }
      });
</script>

<div id="date" style="font-size: 75%;" class="required"></div>
<input type="hidden" name="ship_date" id="ship_date" class="required" />

我重新定位错误消息:

jQuery("#custform").validate({
invalidHandler: function(e, validator) {
    var errors = validator.numberOfInvalids();
    if (errors) {
        var message = errors == 1
            ? 'You missed 1 field. It has been highlighted below'
            : 'You missed ' + errors + ' fields.  They have been highlighted     below';
        $("div.error span").html(message);
        $("div.error").show();
    } else {
        $("div.error").hide();
    }           
},
});

我的目标:当某人在不选择日期的情况下提交表单时,会向他们显示一条消息,要求您输入一个日期,然后再提交表单.如果我们可以在日期选择器的div中添加一个类,那就更好了.

My Goal: When someone submits a form without picking a date, they are presented with a message to enter a date before the form will submit. If we can add a class to the date picker's div, all the better.

感谢阅读

推荐答案

您是否正在使用jQuery验证程序插件?如果是这样,您可以在隐藏字段中添加一个类

Are you using jQuery validator plugin? If so you can add a class to the hidden field

<input type="hidden" class="required" id="field1" name="field1"/>

,然后将验证器选项设置为null,不忽略任何内容...

and set the validator option ignore to null, ignore nothing...

$.validator.setDefaults({ ignore: '' });

(参考) 并且您会在隐藏字段所在的位置收到一条错误消息msg.如果它在日期字段附近,则看起来日期字段具有味精.我以前已经在表格中完成过此操作.

(reference) and you'll get an error msg where the hidden field is. If it's near the date field it will look like the date field has the msg. I've done this in my forms before.

希望这会有所帮助.

这篇关于使用jquery验证来验证内联jquery datepicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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