[英]Regular expression for date validation
I am having a problem in validating the date, In my code I have used a regular expression which accepts dates of year 2012, if I give date having yr as 2013 it says "Invalid date". 我在验证日期时遇到问题,在我的代码中,我使用了一个接受2012年日期的正则表达式,如果我将yr设为2013年,则该日期表示“无效日期”。 Kindly help me on this regard. 在这方面请帮助我。 It should accept any years.. I mean valid years atleast from year 2000 to year 3000. Thanks in advance. 它应该接受任何年份。.我的意思是至少从2000年到3000年为有效年份。在此先感谢。
function checkDates(){
var sdate = "2013-01-02";
var edate = "2013-01-02";
if (!isValidDate(sdate)) {
alert("Report Start Date is Invalid!!");
return false;
}
if (!isValidDate(edate)) {
alert("Report End Date is Invalid!!");
return false;
}
return true;
}
function isValidDate(sText) {
var reDate = /(?:([0-9]{4}) [ -](0[1-9]|[12][0-9]|3[01])[ -]0[1-9]|1[012])/; // yy/mm/dd
return reDate.test(sText);
}
There's an extra space in your regex and missing brackets (bracket issue makes it accept 2012-aa-xx
date: 正则表达式中有多余的空间,缺少括号(括号问题使它接受2012-aa-xx
日期:
/(?:([0-9]{4}) [ -](0[1-9]|[12][0-9]|3[01])[ -]0[1-9]|1[012])/
^ ^
-------------/-------------------------------/
So: 所以:
([0-9]{4}[ -](0[1-9]|[12][0-9]|3[01])[ -](0[1-9]|1[012]))
the below expression also works 下面的表达式也适用
/(?:19|20\d{2})\-(?:0[1-9]|1[0-2])\-(?:0[1-9]|[12][0-9]|3[01])/
Thanks, Dhiraj 谢谢,Dhiraj
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.