View Full Version : Regex Date

09-27-2012, 01: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.

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

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

09-27-2012, 02: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, 03: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


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