javascript 欧芹日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26492367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:01:32  来源:igfitidea点击:

Parsley Date Format

javascriptparsley.js

提问by Hong Yi

I am currently setting up a form with a <input type=date>field with Parsley to validate that the date cannot be in the past. However, I am unable to get it to validate despite giving it the correct values(in the future). It seems to read the minimum value from the min field but I tried changing the formats around(e.g Y/m/d) but the error still pops up. Can I know what is the issue behind this and what is the workaround to it? Thanks in advance.

我目前正在设置一个<input type=date>带有 Parsley 字段的表单,以验证日期不能是过去。但是,尽管给了它正确的值(将来),我仍无法对其进行验证。它似乎从 min 字段中读取最小值,但我尝试更改格式(例如 Y/m/d),但错误仍然弹出。我可以知道这背后的问题是什么以及解决方法是什么?提前致谢。

The error is This value should be greater than or equal to 10/21/2014.even though I gave it a date later than that.

错误是This value should be greater than or equal to 10/21/2014.即使我给它一个晚于那个日期。

<input type="date" name="contact-date" id="contact-date" placeholder="MM/DD/YYYY" data-date-format="mm/dd/yyyy" min="<?php echo date('m/d/Y'); ?>" parsley-type="dateIso">

I looked through K D's answer and realized that the regex was for DD/MM/YYYY so I changed it from

我查看了 KD 的回答并意识到正则表达式是用于 DD/MM/YYYY 所以我将它从

/^([12]\d|0[1-9]|3[01])\D?(0[1-9]|1[0-2])\D?(\d{4})$/to

/^([12]\d|0[1-9]|3[01])\D?(0[1-9]|1[0-2])\D?(\d{4})$/

/^(0[1-9]|1[0-2])\D?([12]\d|0[1-9]|3[01])\D?(\d{4})$/

/^(0[1-9]|1[0-2])\D?([12]\d|0[1-9]|3[01])\D?(\d{4})$/

Somehow this changed to any date passed is a valid date despite the min value being specified.

尽管指定了最小值,但以某种方式更改为任何传递的日期都是有效日期。

Code is now:

代码现在是:

<input type="date" class="contact-input" name="contact-date" id="contact-date" placeholder="MM/DD/YYYY" data-type="dateIso" data-parsley-min="<?php echo date('m/d/Y'); ?>">

回答by Luís Cruz

parsley-type="dateIso"tells me that you are using Parsley v1.*which is deprecated. data-date-format="mm/dd/yyyy"tells me that you are using Bootstrap Datetimepicker.

parsley-type="dateIso"告诉我您正在使用已弃用的Parsley v1.*data-date-format="mm/dd/yyyy"告诉我您正在使用Bootstrap Datetimepicker

I suggest you use Parsley 2.x (even if it you need to craft the validator for min date) and use the attribute data-date-minDateof Datetimepicker.

我建议您使用 Parsley 2.x(即使您需要为最小日期制作验证器)并使用data-date-minDateDatetimepicker的属性。

For this solution, you need the following code (working jsfiddle):

对于此解决方案,您需要以下代码(工作 jsfiddle):

<div class='input-group date' id='datetimepicker'>
    <input type='text' name="contact-date" id="contact-date"
        placeholder="MM/DD/YYYY"
        data-date-format="MM/DD/YYYY"
        data-date-minDate="<?php echo date('m/d/Y'); ?>"
        data-parsley-mindate="<?php echo date('m/d/Y'); ?>" />
    <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
</span>
</div>

<script>
window.ParsleyValidator
    .addValidator('mindate', function (value, requirement) {
        // is valid date?
        var timestamp = Date.parse(value),
            minTs = Date.parse(requirement);

        return isNaN(timestamp) ? false : timestamp > minTs;    
    }, 32)
    .addMessage('en', 'mindate', 'This date should be greater than %s');

$('#myForm').parsley();

$('#datetimepicker').datetimepicker({
    language:'en'
});
</script>

Notes:

笔记:

  • data-date-formatshould be MM/DD/YYYY(uppercase, check momentjs docs);
  • Replace parsley-type="dateIso"with data-parsley-mindate="<?php echo date('m/d/Y'); ?>" />so the custom validator is executed
  • data-date-format应该是MM/DD/YYYY(大写,检查momentjs 文档);
  • 更换parsley-type="dateIso"data-parsley-mindate="<?php echo date('m/d/Y'); ?>" />这样执行自定义验证