jquery验证add方法来验证datetime [英] jquery validate add method to validate datetime

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

问题描述

我正在使用一个datetimepicker插件,我发现这里工作得很好。

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

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

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

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

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.

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

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

谢谢

推荐答案

您已经对 code>规则。否则,您可以查看jQuery Validate插件以获取以下内容:

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

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 time portion of your string.

$.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.");

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

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

只需将这两个功能合并到您的DateTime方法。然后在空格处分割你的输入值,并相应地测试每个部分。

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

这是一个演示,我结合了日期&时间方法在一起它接受以下格式, yyyy / mm / dd hh:mm ,所以你只需要调整一下。

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.

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

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

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

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