jQuery 用于 javascript 验证的 dd/mm/yyyy 格式的日期正则表达式

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

Regular expression for date in dd/mm/yyyy format for javascript validation

jqueryvalidationdate

提问by Ratnesh Lal

I have to do client side validation(using javascript/JQuery) for the input value which should have date in dd/mm/yyyy format. Please share regular expression if exist. It should validate the date is proper in all context.

我必须对应该具有 dd/mm/yyyy 格式的日期的输入值进行客户端验证(使用 javascript/JQuery)。如果存在,请分享正则表达式。它应该验证日期在所有上下文中是否正确。

Thanks in advance, Ratnesh

提前致谢,拉特内什

回答by Serghei

try to use

尝试使用

/^[0-9]{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])/

or look at this the same question

或者看看这个同样的问题

回答by Alex KeySmith

As an alternative to regular expressions you may wish to take a look at datejs

作为正则表达式的替代方案,您可能希望查看 datejs

http://www.datejs.com

http://www.datejs.com

Using parse exact http://code.google.com/p/datejs/wiki/APIDocumentation#parseExact

使用解析精确http://code.google.com/p/datejs/wiki/APIDocumentation#parseExact

you can do things like:

您可以执行以下操作:

var myDate = Date.parseExact("19/12/2004", "dd/mm/yyyy"); 

And will return null, if the date cannot be parsed.

如果无法解析日期,则返回 null。

n.b. I haven't tested the code above, but it should work ok.

nb 我还没有测试上面的代码,但它应该可以正常工作。

回答by Susana Santos

Use

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

I was looking for the same thing and got it from here

我正在寻找同样的东西并从这里得到它

This expression validates days from 01 to 31, months from 01 to 12 but it accepts any number with 4 digts for year (including 0000). And it returns true for wrong dates like 31/02/2015.

此表达式验证从 01 到 31 的天数,从 01 到 12 的月数,但它接受任何带有 4 位数字的年份(包括 0000)。对于错误的日期,例如 31/02/2015,它返回 true。

Use a regex online validator like this onein order to check if it suits you.

使用像这样的正则表达式在线验证器来检查它是否适合您。

回答by Bartosz

Regex will validate your format, but won't validate if date makes logic sense (leap years and stuff). That's said:

正则表达式将验证您的格式,但不会验证日期是否符合逻辑(闰年之类的)。那是说:

Javascript date regex DD/MM/YYYYjavascript regexp, validating date problem

Javascript 日期正则表达式 DD/MM/YYYY javascript正则表达式,验证日期问题

and numerous others...

和许多其他人......