RegularExpression For Date

Posted by Lakhangarg under JavaScript on 12/2/2009 | Views : 1224 | Status : [Member] [Moderator] | Replies : 3
Hi -
i have regular expression for date and that is : ^[0-1]{1,1}\d{1,1}/[0-3]{1,1}\d{1,1}/\d{4}$
there are some issue with the above regular expression as i can enter month value upto 19 and date upto 39. can anybody tell me how we can use oring in regular expression so i can check if month start with 3 then second value can be 0 or 1 only.

Thanks & Regards
Lakhan Pal Garg




Responses

Posted by: Abhi2434 on: 12/2/2009 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
Hey,

I think you might try this :
(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d



www.abhisheksur.com

Lakhangarg, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Lakhangarg on: 12/2/2009 [Member] [Moderator] Silver

Up
0
Down
this will be wrong in case of feb and leap year. and also in case eof year 1800 and so on...
for 1800 we can add 18 as 18|19|20 but what will be the solution for other problem.
one more problem is there that if the number of days in the month are 31 then its fine other wise it will return wrong result too.

Thanks & Regards
Lakhan Pal Garg

Lakhangarg, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rcsprakash on: 1/16/2010 [Member] Starter

Up
0
Down
var dt;
var datesplit;
dt=year+"/"+month+"/"+day;
var entereddate=new Date(dt);
datesplit=dt.split("/");
if(entereddate.getMonth()!=datesplit[1]-1 || entereddate.getDate()!=datesplit[2] ||entereddate.getFullYear()!=datesplit[0])
{
alert('wrong date');
return false;
}


simply pass your date value(year , month and date) this will solve your problem 100%

Lakhangarg, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response