jQuery日期验证(YYYY-MM-DD) [英] jQuery Date Validation (YYYY-MM-DD)

查看:161
本文介绍了jQuery日期验证(YYYY-MM-DD)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用此脚本来验证日期 YYYY-MM-DD,但我似乎无法使其正常运行.使用我使用的正则表达式,它允许用户输入10个不带破折号的数字,而不是8个在正确位置加破折号的数字.有没有办法修改我的脚本来解决此问题?

I need to validate a date using this script in the format of YYYY-MM-DD, but I can't seem to get it working perfectly. With the regular expression I'm using, it allows for users to enter 10 numbers without dashes, instead of 8 with dashes in the correct places. Is there a way to modify my script to fix this?

jQuery.validator.addMethod("date", function(date, element) {
                return this.optional(element) || date.match(/^[-0-9]{10}$/);
            }, "Please specify a valid date");

推荐答案

您输入的正则表达式错误.

You have a wrong regex.

您可以改用这个:

^\d{4}-((0\d)|(1[012]))-(([012]\d)|3[01])$

应该是:

return this.optional(element) || date.match(/^\d{4}-((0\d)|(1[012]))-(([012]\d)|3[01])$/);

这篇关于jQuery日期验证(YYYY-MM-DD)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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