Wrox Home  
Search P2P Archive for: Go

  Return to Index  

html_code_clinic thread: Re: client side validation for date


Message #1 by "Suneel" <psuneel@v...> on Sat, 7 Sep 2002 06:41:54
Hi

/* Function formats the date with "/" as separator
If mm/dd/ccyy, century=true
If mm/dd/yy, century=false */
function CheckDate(theField,century)
{
	var a;
	if (IsValidDate(Trim(theField.value),century))
	{
		a = Trim(theField.value);
		b = a.substring(0,a.indexOf("/")); // month
		if (b != '')
			d = a.substring(b.length+1,a.indexOf("/",(a.indexOf
("/")+1))); // day
		else 
			{	
				b = a.substring(0,a.indexOf("-")); // month
	
				d = a.substring(b.length+1,a.indexOf("-",
(a.indexOf("-")+1))); // day
			}
		f = a.substring(b.length+d.length+2, a.length)// year
		if (f.length > 4)return false;
		if (century == false)
		{
			if (f.length == 1 || f.length == 2)
				if (f >= 20)
					f = parseFloat(f) + 1900;
				else
					f = parseFloat(f) + 2000;
		}
		if (b.length == 1) b = "0" + b;
		if (d.length == 1) d = "0" + d;
		theField.value = b + "/" + d + "/" + f;
		if((century == false) && (f < 1920))  //ksn
		{
		  return false;
		}
		else
		{
		  return true;
		}
	}
	else
	{
		return false;
	}	
}

function IsValidDate(dtDate,century)
{
	a = dtDate;
	err=0;
	if(a.length > 0)
	{
		if (century == true)
			if ((a.length < 8) || (a.length >10)) err=1;
		else
			if ((a.length < 5) || (a.length >10)) err=1;
		b = a.substring(0,a.indexOf("/")); // month
		if (b != '')
		{
			if (!isNumber(b))err = 1;
			d = a.substring(b.length+1,a.indexOf("/",(a.indexOf
("/")+1))); // day
		}
		else 
		{	
			b = a.substring(0,a.indexOf("-")); // month	
			if (!isNumber(b))err = 1;
			d = a.substring(b.length+1,a.indexOf("-",(a.indexOf
("-")+1))); // day
		}
		if (d<1 || d>31) err = 1;
		if (!isNumber(d))err = 1;
		f = a.substring(b.length+d.length+2, a.length)// year
		if (!isNumber(f))
		{
			err = 1;
		}
	
		//basic error checking
		if (b<1 || b>12) err = 1;
		if (f.length == 3) err = 1;
		if ((century == true) && (f < 1900)) err = 1;
		
		//advanced error checking
		// months with 30 days
		if (b==4 || b==6 || b==9 || b==11)
		{
		  if (d==31) err=1;
		}
		// february, leap year
		if (b==2)
		{
			var g=parseInt(f/4);
			if (!isNumber(g)) err=1;
			if (d>29) err=1;
			if (d==29 && ((f/4)!=parseInt(f/4))) err=1;
		}
		if (err==1)
		{
		 return false;
		}
		else
		{
		 return true;
		}
									
		
	}
} 

Regards
Suneel

> Hi all at p2p,
> 
> I get the date in the html page and insert it into a
> database(ms access).The date should be in the format
> (dd/mm/yy) inside the database. can any validation be
> done in the html page (using javascript) to get the date
> in the specified format from the user before it is 
> submitted.
> 
> please help me.
> Thanks in advance.
> --priya

  Return to Index