Date Format Validator : Date Validation « Development « JavaScript DHTML

Home
JavaScript DHTML
1.Ajax Layer
2.Data Type
3.Date Time
4.Development
5.Document
6.Dojo toolkit
7.Event
8.Event onMethod
9.Ext JS
10.Form Control
11.GUI Components
12.HTML
13.Javascript Collections
14.Javascript Objects
15.Javascript Properties
16.jQuery
17.Language Basics
18.Mochkit
19.Mootools
20.Node Operation
21.Object Oriented
22.Page Components
23.Rico
24.Scriptaculous
25.Security
26.SmartClient
27.Style Layout
28.Table
29.Utilities
30.Window Browser
31.YUI Library
JavaScript DHTML » Development » Date Validation 




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
// [email protected]

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>|| 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">
&#1692002 <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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.