.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Imran Ghani

Home >> Code Snippets >> Javascript >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Date validation in Javascript

Posted By:Athira Sreejith       Posted Date: March 19, 2009    Points: 10    Category: Javascript    URL: http://www.dotnetspark.com  

This function is used to validate Date using Javascript in client side.

function isDate(dtStr){
 var daysInMonth = DaysArray(12)
 var pos1=dtStr.indexOf(dtCh)
 var pos2=dtStr.indexOf(dtCh,pos1+1)
 var strMonth=dtStr.substring(0,pos1)
 var strDay=dtStr.substring(pos1+1,pos2)
 var strYear=dtStr.substring(pos2+1)
 if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
 if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
 for (var i = 1; i <= 3; i++) {
  if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
 if (pos1==-1 || pos2==-1){
  alert("The date format should be : mm/dd/yyyy")
  return false
 if (strMonth.length<1 || month<1 || month>12){
  alert("Please enter a valid month")
  return false
 if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
  alert("Please enter a valid day")
  return false
 if (strYear.length != 4 || year==0 || yearmaxYear){
  alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
  return false
 if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
  alert("Please enter a valid date")
  return false
return true


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend