-
Date Format Validator <javascript>
//**************************************
//
// Name: Date Format Validator
// Description:This script validates the
// date that the user inputs for the follow
// ing sample format:
23 Feb 1983
// By: Premshree Pillai
//
//This code is copyrighted and has// limited warranties.Please see http://
// www.Planet-Source-Code.com/vb/scripts/Sh
// owCode.asp?txtCodeId=3092&lngWId=2//for details.//**************************************
//
<!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>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>
<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:
<FONT color="#808080">23 Feb 1983</FONT>
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"> Date Format Validator </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'); ">
<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">
© 2002 <FONT color="#808080">Premshree Pillai</FONT>.
</FONT>
</TD></TR></TABLE>
</BODY>
</HTML>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
Bookmarks