如何在jQuery验证中将此日期设置为可选? [英] How can I make this date optional in jQuery Validation?

查看:63
本文介绍了如何在jQuery验证中将此日期设置为可选?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在以前需要的日期字段上有一个日期选择器.我试图使其不再需要,但是我没有看到如何做到这一点.

I have a datepicker on a previously required date field. I am attempting to make it no longer required, but I'm not seeing how to do that.

该表单正在使用 jQuery验证.我看到有一个可以设置为false的必填属性,但是错误仍然出现.这是我正在使用的代码:

The form is using jQuery Validation. I see there is a required attribute that you can set to false, but the error appears still. Here is the code I am using:

setUpValidationForDates: function() {
  $('input.datepicker').each(function(i, el) {
    $.validator.addMethod('sDateFormat', function(value, element, param) {
      return String(value).match(/^[0-9]{4}-[0-9]{2}-[0-9]{2}/);
    }, 'Must have format YYYY-MM-DD');
    $(el).rules('add', {
      required: false,
      sDateFormat: true,
      validDateString: true,
    });
  });
}

该字段为空时,将显示有关必须格式为YYYY-MM-DD"的消息.我需要它来验证是否存在值,但允许空值.我如何根据需要完成此操作:false似乎并没有达到我的预期.

The message seen about, "Must have format YYYY-MM-DD" is showing when the field is empty. I need it to validate if a value is present, but allow empty values. How can I accomplish this, as required: false does not seem to be working as I would expect.

谢谢

推荐答案

您已经编写了自定义方法,好像该字段是required ...,这就是为什么您在字段仍然静止时收到错误消息的原因空的.

You've written your custom method as if the field is required... that is why you're getting the error message while the field is still empty.

您需要按如下所示将this.optional(element) ||添加到自定义方法中...

You would need to add this.optional(element) || to your custom method as follows...

$.validator.addMethod('sDateFormat', function(value, element, param) {
    return this.optional(element) || String(value).match(/^[0-9]{4}-[0-9]{2}-[0-9]{2}/);
}, 'Must have format YYYY-MM-DD');

现在,除非在相关字段中输入内容,否则自定义方法将不会触发.

Now the custom method will not fire unless something is entered into the relevant field.

对自定义方法的此编辑只是使其变为可选".换句话说,如果您以后希望该字段为required,只需将required: true添加到规则声明中,而无需使用此自定义方法.

This edit to the custom method simply makes it "optional". In other words, if you later wanted the field to be required, simply add required: true to the rule declaration without touching this custom method.

第二,您只需要声明addMethod 一次 即可创建新方法.应该将它从您的.each()中删除,因为不必要重复调用它.您也可以删除required: false,因为它已经是required规则的默认行为.

Secondly, you only need to declare addMethod once to create the new method. It should be taken out of your .each() as it's unnecessarily being called repeatedly. You can also remove required: false as it's already the default behavior of the required rule.

setUpValidationForDates: function() {

    $.validator.addMethod('sDateFormat', function(value, element, param) {
        return this.optional(element) || String(value).match(/^[0-9]{4}-[0-9]{2}-[0-9]{2}/);
    }, 'Must have format YYYY-MM-DD');

    $('input.datepicker').each(function(i, el) {
        $(el).rules('add', {
            // required: false, // don't need this anymore, it's default
            sDateFormat: true,
            validDateString: true
        });
    });

}

这篇关于如何在jQuery验证中将此日期设置为可选?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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