jquery validate add 方法来验证日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15787503/
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-08-26 15:48:35  来源:igfitidea点击:

jquery validate add method to validate datetime

jqueryjquery-uijquery-validatedatepicker

提问by John the Ripper

I'm using a datetimepicker plugin that I found herewhich works quite well.

我正在使用我在此处找到的日期时间选择器插件,该插件运行良好。

The only problem now is that it breaks the standard date validation included with the jquery validation pluginbecause of the added time on the end of the string in the input box.

现在唯一的问题是它破坏了jquery 验证插件中包含的标准日期验证,因为在输入框中的字符串末尾增加了时间。

Here is what I have so far in a demo. I added a validation method that checks if the value is a date, but it doesn't accept the time.

这是我到目前为止在演示中的内容。我添加了一个验证方法来检查该值是否为日期,但它不接受时间。

I need help with writing the custom validation method. How do I split the dateand timestring and then perform the tests to validate them?

我需要帮助编写自定义验证方法。如何拆分datetime字符串,然后执行测试以验证它们?

Thanks

谢谢

回答by Sparky

You already have validation for the daterule. Otherwise, you can look inside the jQuery Validate plugin to obtain that:

您已经对date规则进行了验证。否则,您可以查看 jQuery Validate 插件内部以获取:

date: function( value, element ) {
    return this.optional(element) || !/Invalid|NaN/.test(new Date(value).toString());
},

Here is a custom method I found for validating the timeportion of your string.

这是我找到的用于验证time字符串部分的自定义方法。

$.validator.addMethod("time", function (value, element) {
    return this.optional(element) || /^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$/i.test(value);
}, "Please enter a valid time.");

Time Demo: http://jsfiddle.net/9CdvN/

时间演示:http: //jsfiddle.net/9CdvN/

Simply combine the two functions into your "DateTime" method. Then split your input value at the space and test each part accordingly.

只需将这两个函数组合到您的“DateTime”方法中即可。然后在空间分割您的输入值并相应地测试每个部分。



Here's a demo where I've combined the Date & Time methods together. It accepts the following format, yyyy/mm/dd hh:mm, so you'll simply need to tweak it a bit.

这是我将日期和时间方法组合在一起的演示。它接受以下格式,yyyy/mm/dd hh:mm因此您只需稍微调整一下即可。

http://jsfiddle.net/9CdvN/2/

http://jsfiddle.net/9CdvN/2/

回答by phonicx

The datetime picker you mentioned says it uses jQuery UI's datepicker widget. You can control the format of the date you receive from the widget using the ui datepicker API, specifically the dateFormat method.

您提到的日期时间选择器说它使用 jQuery UI 的日期选择器小部件。您可以使用 ui datepicker API 控制从小部件接收的日期格式,特别是dateFormat 方法

You should be able to set the datepicker up to supply a date in the format that you need for your validation.

您应该能够将日期选择器设置为以验证所需的格式提供日期。

Hope that helps!

希望有帮助!

回答by Kristopher

Might I suggest you switch plugins? http://www.datejs.com/has an outstanding number of ways to validate dates and some ridiculous syntactic sugar:

我可以建议你切换插件吗?http://www.datejs.com/有很多方法来验证日期和一些荒谬的语法糖:

// Convert text into Date
Date.parse('today');
Date.parse('t + 5 d'); // today + 5 days
Date.parse('next thursday');
Date.parse('February 20th 1973');
Date.parse('Thu, 1 July 2004 22:30:00');