Date Validation via Regular Expression

The function takes an input string and an optional format string. The format string is only 3 characters long and determines the relative positions of the month, day, and year. If the format string is omitted, then it will be “MDY” (month first, then day, then year). There are three different types of dividers that can be used in the date string. These are the slash (/), the period (.) and the dash (-). Years can be either 2 digits (00-49 are assumed to be 21st century and 50-99 are assumed to be 20th century) or 4 digits.

The way the regular expression part of the function works is by using the “remember” capabilities of regular expressions. This assures that the same divider is used in both positions. (In other words, you couldn’t do MM/DD-YYYY). The “\1” in the regular expression says “use the same thing you used in parentheses before (which is the check for a slash, period, or dash) and apply the check here”.

If the string passes the regular expression (there are 2 checks since the year can be either 2 or 4 digits), then additional JavaScript is performed to check the validity of the date. Instead of doing all the checks for the day of the month being out of range and checking for leap years, a simple approach is taken. A new date object is created in JavaScript. If the numbers are out of range (like February 31), JavaScript will still create an object, it will just be adjusted to be a valid date. So create the date and then check to see if it was adjusted by JavaScript. If it was adjusted, then the original date is not a valid date.

function isValidDate(dateStr, format)
{

//check if 2nd parameter contains valid value or not
//if not valid then set default format = “MDY”

if (format == null) { format = “MDY” }
format = format.toUpperCase();
if (format.length != 3) { format = “MDY” }
if ( (format.indexOf(“M”) == -1) ||
(format.indexOf(“D”) == -1) ||
(format.indexOf(“Y”) == -1)
)
{ format = “MDY” }

if (format.substring(0, 1) == “Y”)
{ // If the year is first
var reg1 = /^\d{2}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
var reg2 = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
}
else if (format.substring(1, 2) == “Y”)
{ // If the year is second
var reg1 = /^\d{1,2}(\-|\/|\.)\d{2}\1\d{1,2}$/
var reg2 = /^\d{1,2}(\-|\/|\.)\d{4}\1\d{1,2}$/
}
else
{ // The year must be third
var reg1 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2}$/
var reg2 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
}

// If it doesn’t conform to the right format
//(with either a 2 digit year or 4 digit year), fail

if ( (reg1.test(dateStr) == false) &&
(reg2.test(dateStr) == false)
)
{ return false; }

// Split into 3 parts based on what the divider was
var parts = dateStr.split(RegExp.$1);

// Check to see if the 3 parts end up making a valid date
//extract month part

if (format.substring(0, 1) == “M”) { var mm = parts[0]; }
else if (format.substring(1, 2) == “M”) { var mm = parts[1]; }
else { var mm = parts[2]; }

//extract day part
if (format.substring(0, 1) == “D”) { var dd = parts[0]; }
else if (format.substring(1, 2) == “D”) { var dd = parts[1]; }
else { var dd = parts[2]; }

//extract Year part
if (format.substring(0, 1) == “Y”) { var yy = parts[0]; }
else if (format.substring(1, 2) == “Y”) { var yy = parts[1]; }
else { var yy = parts[2]; }

//if year is in 2 digit
//00-49 are assumed to be 21st century and 50-99 are assumed to be 20th century
if (parseFloat(yy) <= 50)
{ yy = (parseFloat(yy) + 2000).toString(); }
if (parseFloat(yy) <= 99) { yy = (parseFloat(yy) + 1900).toString(); }

var dt = new Date(parseFloat(yy), parseFloat(mm)-1, parseFloat(dd), 0, 0, 0, 0);

if (parseFloat(dd) != dt.getDate()) { return false; }

if (parseFloat(mm)-1 != dt.getMonth()) { return false; }

return true;

}

The function returns true if the date is valid and false if it is not. So, your validation could look something like:

if (!isValidDate(myDateString, “DMY”)) { alert(“The date is not in the correct format.”); }

Reference:
Breaking Par Consulting Inc.

happy programming!!!

Advertisements

One Response to Date Validation via Regular Expression

  1. Anil says:

    i want some more comment in date validation.i am in software testing field so if any validation query is there so pls send to mail.

    Thanks
    Anil

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: