0

Here is the date validation I am currently using, it works, but I want to make sure 2 digits are entered for month and 2 for Day. I am not too familiar with javascript, and getting this far took lots of searching on here. Any help would be appreciated. Thanks

 function validate_birthday(field,alerttxt)
 {  
 with (field)
 {
 dateParts = value.split('-');
 if(dateParts.length != 3) {
 alert('Date Format Must Be MM-DD-YYYY');
 return false; }
  testDate = new Date(dateParts[2] + '/' + dateParts[1] + '/' + dateParts[0]);  

if(isNaN(testDate.getDate())) {     alert('Date Format Must Be MM-DD-YYYY');     return false; 

 } 
 else {return true;}
}
}
user1232073
  • 21
  • 1
  • 2
  • 5
  • I would use a regular expression. See this post: [http://stackoverflow.com/questions/5465375/javascript-date-regex-dd-mm-yyyy][1] [1]: http://stackoverflow.com/questions/5465375/javascript-date-regex-dd-mm-yyyy – BJ Safdie Mar 02 '12 at 00:15

1 Answers1

1

Try using a regular expression instead to match on the date format.

/[0-1]{1}[0-9]{1}-[0-3]{1}[0-9]{1}-2[0-9]{3}/

or a simpler way simply checking for 2 numbers a hyphen 2 numbers a hyphen and 4 numbers:

/[0-9]{2}-[0-9]{2}-[0-9]{4}/
Dave Snigier
  • 2,574
  • 3
  • 21
  • 29