var pattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
function validateDate(userDate) {
var fld = document.getElementById(userDate);
if(fld.value != "") {
if(!fld.value.match(pattern)) {
alert("Date must be in mm/dd/yyyy format");
}
}
}
Monday, January 14, 2008
Date Validation Using Javascript in ASP.Net
I ran across an issue today where I had to use javascript to validate the user date. The pattern I matched against is "mm/dd/yyyy". Below is the code that does the trick
Subscribe to:
Post Comments (Atom)
1 comments:
Hi,
Date validation in asp.net textbox has always been a genuine need. Although we could do it using validator control in asp.net, use of javascript is always appreciated. Here goes another simple method: http://dotnetspidor.blogspot.com/2009/03/using-javascript-for-validation-of.html
Happy Programming!
Post a Comment