Date Format Validator : Date Validation « Development « JavaScript DHTML






Date Format Validator

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Date Format Validator</title>
<script language="JavaScript">
// Date Format Validator
// Sample Format : 23 Feb 1983
// (c) 2002 Premshree Pillai
// http://www.qiksearch.com
// qiksearch@rediffmail.com

function validate_date(formName, textName)
{
 var errMsg="", lenErr=false, dateErr=false;
 var testObj=eval('document.' + formName + '.' + textName + '.value');
 var testStr=testObj.split(' ');
 if(testStr.length>3 || testStr.length<3)
 {
  lenErr=true;
  dateErr=true;
  errMsg+="There is an error in the date format.";
 }
 var monthsArr = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug" ,"Sep", "Oct", "Nov", "Dec");
 var daysArr = new Array;
 for (var i=0; i<12; i++)
 {
  if(i!=1)
  {
   if((i/2)==(Math.round(i/2)))
   {
    if(i<=6)
    {
     daysArr[i]="31";
    }
    else
    {
     daysArr[i]="30";
    }
   }
   else
   {
    if(i<=6)
    {
     daysArr[i]="30";
    }
    else
    {
     daysArr[i]="31";
    }
   }
  }
  else
  {
   if((testStr[2]/4)==(Math.round(testStr[2]/4)))
   {
    daysArr[i]="29";
   }
   else
   {
    daysArr[i]="28";
   }
  }
 } 
 var monthErr=false, yearErr=false;
 if(testStr[2]<1000 && !lenErr)
 {
  yearErr=true;
  dateErr=true;
  errMsg+="\nThe year \"" + testStr[2] + "\" is not correct.";
 }
 for(var i=0; i<12; i++)
 {
  if(testStr[1]==monthsArr[i])
  {
   var setMonth=i;
   break;
  }
 }
 if(!lenErr && (setMonth==undefined))
 {
  monthErr=true;
  errMsg+="\nThe month \"" + testStr[1] + "\" is not correct.";
  dateErr=true;
 }
 if(!monthErr && !yearErr && !lenErr)
 {
  if(testStr[0]>daysArr[setMonth])
  {
   errMsg+=testStr[1] + ' ' + testStr[2] + ' does not have ' + testStr[0] + ' days.';
   dateErr=true;
  }
 }
 if(!dateErr)
 {
  eval('document.' + formName + '.' + 'submit()');
 }
 else
 {
  alert(errMsg + '\n____________________________\n\nSample Date Format :\n23 Feb 1983');
  eval('document.' + formName + '.' + textName + '.focus()');
  eval('document.' + formName + '.' + textName + '.select()');
 }
}
</script>
</head>
<body bgcolor="#FFFFFF">

<center><span style="font-family:verdana,arial,helvetica; font-weight:bold; font-size:15pt; color:#FF9900; filter:DropShadow(color=#110000, offX=1, offY=1, positive=1); width:100%">Date Format Validator</span></center>

<br>
<table width="300" align="center"><tr><td>
<font face="verdana,arial,helvetica" size="-1">
This script validates the date that the user inputs for the following sample format:

<br><font color="#808080">23 Feb 1983</font>
<br><br>Try it yourself !
<form name="form1" method="" action="">
<fieldset style="width:200px; border:#808080 solid 1px">
<legend align="left" width="330"><font face="verdana,arial,helvetica" size="-2" color="#808080">&nbsp;Date Format Validator&nbsp;</font></legend>
<table><tr><td></td></tr></table>
<center>
<input type="text" maxlength="11" style="border:#000000 solid 1px; width:120px" name="date">
<input type="button" value="Submit" style="border:#000000 outset 1px; background:#EFEFEF; height:20px; cursor:pointer" onClick="javascript:validate_date('form1','date');">
<br><font size="-2" color="#808080">(Click on the button to submit)</font>
</center>
<table><tr><td></td></tr></table>
</fieldset>
</form>

<hr style="color:#FF9900; height:1px">
&#169; 2002 <a href="http://www.qiksearch.com" title="Click here to visit Qiksearch.com"><font color="#808080">Premshree Pillai</font></a>.
</font>
</td></tr></table>

</body>
</html>

           
       








Related examples in the same category

1.Date Validation in a Form