如何验证三个单独的字段作为日期 [英] How to validate three separate fields as a date

查看:69
本文介绍了如何验证三个单独的字段作为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试验证一个分为三个字段(当然是年,月和日)的日期.我无法根据需要更改这些字段.我正在使用 jQuery验证插件(同样不是我的选择).我无法弄清楚如何将所有三个字段都验证为一个.甚至单独验证它们也足够.

I am trying to validate a date that is separated into three fields (year, month, and day, of course). I cannot change these fields, as much as I'd like to. I am using the jQuery Validation plugin (also not by my choice). I cannot figure how to validate all three fields as one. Even validating them individually would suffice.

我的HTML如下.

<div class="date-field requiredField">
    <div class="date-picker hasDatepicker" id="dp1412187717156" style="display: none;">...</div>
    <input type="text" class="month" required="required" maxlength="2" placeholder="MM" title="Month" value="1">
    &nbsp;/&nbsp;
    <input type="text" class="day" required="required" maxlength="2" placeholder="DD" title="Day" value="1">
    &nbsp;/&nbsp;
    <input type="text" class="year" required="required" placeholder="YYYY" title="Year" value="1973">
    &nbsp;
    <span class="date-pick-button"></span>
    <span class="date-clear-button"></span>
</div>

我正在按如下方式调用验证器:

I am invoking the validator as follows:

$("#SignUp_Enrollment").validate({
    focusInvalid : false,
    rules: {
        co_ssn: {
            ssn: true
        },
        first_name: {
            required: true
        },
        last_name: {
            required: true
        }
        // some more rules
    },
    invalidHandler: function (form, validator) {
        // style the invalid fields
    },
    submitHandler: function (form) {
        form.submit();
    }
});

我当然正在验证其他字段,但是此插件并没有给我任何怜悯.我尝试添加月,日和年规则无济于事.关于采取什么方向有什么建议吗?

I am of course validating other fields, but this plugin is not showing me any mercy. I've tried adding month, day, and year rules to no avail. Any suggestions on what direction to take?

推荐答案

引用OP:

即使单独验证它们也足够."

您的代码已损坏,因为HTML标记中的任何字段都不包含name属性.为了使此插件能够运行,必须使用唯一的name.这就是它跟踪输入元素的方式.

Your code is broken because none of the fields in your HTML markup contain a name attribute. A unique name is mandatory in order for this plugin to operate. It's how it keeps track of the input elements.

请参阅: http://jqueryvalidation.org/reference/#markup-recommendations

它没有显示在您的标记中,但是所有相关的输入元素也必须包含在<form></form>标记内.否则该插件将无法正常工作.

It's not shown in your markup, but all relevant input elements must also be contained within <form></form> tags. This plugin will not work otherwise.

由于您已经将日期保存到隐藏字段中,因此您也可以只验证隐藏字段.

Since you already have the date saved into an hidden field, you can also just validate the hidden field.

<input type="hidden" name="myDate" />

使用ignore: []选项激活对隐藏字段的验证.

Activate validation on hidden fields by using the ignore: [] option.

然后,只要此隐藏字段具有唯一的name属性,您就可以像在其他任何字段上一样应用验证规则.

Then as long as this hidden field has a unique name attribute, you can apply validation rules the same as you would on any other field.

使用errorPlacement回调将邮件附加到隐藏字段中.

Use the errorPlacement callback to precisely place the message attached to the hidden field.

这篇关于如何验证三个单独的字段作为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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