The simplest way to validate a date in mm/dd/ccyy format in JavaScript is to load it into a date object and then test to make sure that the day, month and year in the date object are the same as those in the original string.
Code:
function isValidDate(dateString)
{
// First check for the pattern
if(!/^\d{2}\/\d{2}\/\d{4}$/.test(dateString))
return false;
// Parse the date parts to integers
var parts = dateString.split("/");
var day = +parts[1];
var month = +parts[0]-1;
var year = +parts[2];
if(year < 1000 || year > 3000) return false;
var dt = new Date(year, month, day);
if (dt.getDate() !== day || dt.getMonth() !== month || dt.getFullYear() !== year) return false;
return true;
}
You could shorten the code slightly more by substituting a string.match for the regexp.test and string.split so as to both validate that the code contains the three numeric values and split them into an array in a single command.
Are you sure that you are only passing the date without any leading or trailing spaces? Since you don't trim off any spaces a single space added to the date would cause it to fail validation.