...

View Full Version : Regex Date



beginner_18
09-27-2012, 02:59 PM
hi all,
can anyone give me regex for date validation in the format dd/mm/yyyy. it should consider all cases 30,31 days, leap years etc..
it should validate present and future days only, past date should not be allowed.
Any help would be appreciated.
Thanks in advance.

devnull69
09-27-2012, 03:07 PM
A regex cannot handle those cases, it can only take care of general string content (with little to no logic)

WolfShade
09-27-2012, 03:24 PM
Actually, I have seen RegEx mask that will take all of that into consideration, but the string is a mile long.

devnull69
09-27-2012, 03:55 PM
Wow ok ... some people can do magic with regexes. But I also saw people turning a tv into a toaster :-)

Philip M
09-27-2012, 04:34 PM
Try this:-



<script type = "text/javascript">

function checkValidDate(yr,mmx,dd) {

// Is year sensible?

if (yr <1910 || yr >2012) { // you may want to change 2012 to some other year!
alert ("Year is out of range")
return false;
}

// Is the date a valid date?

mm = mmx-1; // remember that in Javascript date objects the months are 0-11
var nd = new Date();
nd.setFullYear(yr,mm,dd); // format YYYY,MM(0-11),DD

var ndmm = nd.getMonth();
if (ndmm != mm) {
alert (dd + "/" + mmx + "/" + yr + " is an Invalid Date!");
return false;
}
else {
alert (dd + "/" + mmx + "/" + yr + " is a Valid Date");
}

// Is the date today or after?

var today = new Date();
if (nd < today) {
alert ("Date must be today or after");
return false;
}

}

// Usage
checkValidDate(2013,2,31) // 31st February 2013

</script>


Quizmaster: Charles who is said to be the father of modern electronic computing?
Contestant: Darwin.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum