jQuery 验证日期时间格式的正则表达式 (MM/DD/YYYY)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15196451/
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
regular expression to validate datetime format (MM/DD/YYYY)
提问by Supreet
I am trying to validate datetime format MM/DD/YYYY. Here is the code I am trying please help.
我正在尝试验证日期时间格式 MM/DD/YYYY。这是我正在尝试的代码,请帮忙。
function ValidateDate(testdate) {
var Status
var reg = /^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/g;
if (!reg.test(testdate)) {
Status = false;
}
return Status;
}
回答by ntheitroadbizarre
Try your regex with a tool like http://jsregex.com/(There is many) or better, a unit test.
使用类似http://jsregex.com/(有很多)或更好的工具(单元测试)尝试您的正则表达式。
For a naive validation:
对于天真的验证:
function validateDate(testdate) {
var date_regex = /^\d{2}\/\d{2}\/\d{4}$/ ;
return date_regex.test(testdate);
}
In your case, to validate (MM/DD/YYYY), with a year between 1900 and 2099, I'll write it like that:
在您的情况下,要验证 (MM/DD/YYYY),年份在 1900 年到 2099 年之间,我会这样写:
function validateDate(testdate) {
var date_regex = /^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/ ;
return date_regex.test(testdate);
}
回答by Sujeet Sinha
The answer marked is perfect but for one scenario, where in the dd and mm are actually single digits. the following regex is perfect in this case:
标记的答案是完美的,但在一种情况下,dd 和 mm 实际上是个位数。在这种情况下,以下正则表达式是完美的:
function validateDate(testdate) {
var date_regex = /^(0?[1-9]|1[0-2])\/(0?[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/ ;
return date_regex.test(testdate);
}
回答by luisnicg
based on this
基于此
I modified the original to this:
我将原文修改为:
^(?:(?:(?:0?[13578]|1[02]|(?:Jan|Mar|May|Jul|Aug|Oct|Dec))(\/|-|\.)31)|(?:(?:0?[1,3-9]|1[0-2]|(?:Jan|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))(\/|-|\.)(?:29|30)))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:(?:0?2|(?:Feb))(\/|-|\.)(?:29)(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0?[1-9]|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep))|(?:1[0-2]|(?:Oct|Nov|Dec)))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])(?:(?:1[6-9]|[2-9]\d)?\d{2})$
回答by Nouman Ali
In this case, to validate Date (DD-MM-YYYY) or (DD/MM/YYYY), with a year between 1900 and 2099,like this with month and Days validation
在这种情况下,要验证日期 (DD-MM-YYYY) 或 (DD/MM/YYYY),年份在 1900 到 2099 之间,就像这样使用月和天验证
if (!Regex.Match(txtDob.Text, @"^(0[1-9]|1[0-9]|2[0-9]|3[0,1])([/+-])(0[1-9]|1[0-2])([/+-])(19|20)[0-9]{2}$").Success)
{
MessageBox.Show("InValid Date of Birth");
txtDob.Focus();
}
回答by MIIB
var pattern = new RegExp((0|1)[0-9]\/[0-3][0-9]\/(19|20)[0-9]{2});
if(!testdate.match(pattern))
return false;
else return true;
回答by kavinder
you can look into thislink and I hope this may be of great help as its was for me.
您可以查看此链接,我希望这对我有很大帮助。
If you are using the regex given in the above link in Java code then use the following regex
如果您在 Java 代码中使用上述链接中给出的正则表达式,请使用以下正则表达式
"^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\\d\\d$"
"^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\\d\\d$"
As I noticed in your example you are using js function then this use regex
正如我在您的示例中注意到的,您使用的是 js 函数,然后使用正则表达式
"^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$"
"^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$"
This will match a date in mm/dd/yyyy format from between 1900-01-01 and 2099-12-31, with a choice of four separators. and not to forget the
这将匹配 1900-01-01 和 2099-12-31 之间的 mm/dd/yyyy 格式的日期,可选择四个分隔符。并且不要忘记
return regex.test(testdate);
return regex.test(testdate);
回答by Gustavo Rossi Muller
It's long, but great:
它很长,但很棒:
new RegExp(/(?=\d)^(?:(?!(?:10\D(?:0?[5-9]|1[0-4])\D(?:1582))|(?:0?9\D(?:0?[3-9]|1[0-3])\D(?:1752)))((?:0?[13578]|1[02])|(?:0?[469]|11)(?!\/31)(?!-31)(?!\.31)|(?:0?2(?=.?(?:(?:29.(?!000[04]|(?:(?:1[^0-6]|[2468][^048]|[3579][^26])00))(?:(?:(?:\d\d)(?:[02468][048]|[13579][26])(?!\x20BC))|(?:00(?:42|3[0369]|2[147]|1[258]|09)\x20BC))))))|(?:0?2(?=.(?:(?:\d\D)|(?:[01]\d)|(?:2[0-8])))))([-.\/])(0?[1-9]|[12]\d|3[01])(?!0000)((?=(?:00(?:4[0-5]|[0-3]?\d)\x20BC)|(?:\d{4}(?!\x20BC)))\d{4}(?:\x20BC)?)(?:$|(?=\x20\d)\x20))?((?:(?:0?[1-9]|1[012])(?::[0-5]\d){0,2}(?:\x20[aApP][mM]))|(?:[01]\d|2[0-3])(?::[0-5]\d){1,2})?$/);