Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Validation

Status
Not open for further replies.

hem3698

Programmer
Jun 17, 2005
9
GB
Hi all,
i need problem with validating date
here is the function i used for date validation.
//
function isDateValid(title,start) {
if(start.value.length < 10) {
alert('The '+title+' Start Date is too short.');
start.focus();
return false;
}

if(!dateFormatValidation(start,false,title+' Start Date')) {
start.focus();
return false;
}
return true;
}


function validateEffectiveDates() {
var title = 'Effective';
var start = document.myform.effectiveStartDate;
return isDateValid(title,start);
}
//

but now the problem is if the enter the date 7/06/2005 or 05/2/1970, it says the the date is too short.

how can i achieve validation for these with the same caode instead of entering 07/06/2005 and 05/02/1970?

regards and thanks in advance
 
Below is what I use, on the onChange or onBlur events of a control. I.E.,
Control Code
Code:
<input name="txtDate" type="text" value="01/07/2003" maxlength="10" id="txtMCertsDate" tabindex="3" onChange="Javascript:ValidateDate(document.formName.txtDate);" onBlur="Javascript:ValidateDate(document.formName.txtDate);" />

Javascript
Code:
function ValidateDate(field)
{
	if (!(isValidDate(field.value.toString())))
	{
		window.alert(warningString);
		event.returnValue = false;
		SetFocus(field);
		warningString = "ERROR:\n\r\n\r";
	}
}

function stripCharsInBag(s, bag)
{
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) 
        {
            returnString += c;
        }
    }
    return returnString;
}

function daysInFebruary (year)
{
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) 
{
	for (var i = 1; i <= n; i++) 
	{
		this[i] = 31;
		if (i==4 || i==6 || i==9 || i==11) 
		{
		    this[i] = 30;
		}
		if (i==2) 
		{
		    this[i] = 29;
		}
   } 
   return this
}

function isValidDate(dateString)
{
	var valid = true;
	var dtCh= "/";
	var daysInMonth = DaysArray(12);
	var pos1=dateString.indexOf(dtCh);
	var pos2=dateString.indexOf(dtCh,pos1+1);
	var strDay=dateString.substring(0,pos1);
	var strMonth=dateString.substring(pos1+1,pos2);
	var strYear=dateString.substring(pos2+1);
	strYr=strYear;
	if (dateString.length > 0)
	{
		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);
			}
		}
		month=parseInt(strMonth);
		day=parseInt(strDay);
		year=parseInt(strYr);
		if (pos1==-1 || pos2==-1)
		{
			warningString += "The date format should be : dd/mm/yyyy\n\r";
			valid = false;
		}
		if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
		{
			warningString += "Please enter a valid day\n\r";
			valid = false;
		}
		if (strMonth.length<1 || month<1 || month>12)
		{
			warningString += "Please enter a valid month\n\r";
			valid = false;
		}
		if (strYear.length != 4 || year==0)
		{
			warningString += "Please enter a valid 4 digit year\n\r";
			valid = false;
		}
		if (dateString.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dateString, dtCh))==false)
		{
			warningString += "Please enter a valid date\n\r";
			valid = false;
		}
	}
	return valid;
}

function SetFocus(field)
{
	field.focus();
	field.select();
}

Rhys

"Vampireware /n/,a project, capable of sucking the lifeblood out of anyone unfortunate enough to be assigned to it, which never actually sees the light of day, but nonetheless refuses to die."

"I see dead pixels!
 
NB: I should add that the above is not all my code but from a mixture of sources, which include myself.

Rhys

"Vampireware /n/,a project, capable of sucking the lifeblood out of anyone unfortunate enough to be assigned to it, which never actually sees the light of day, but nonetheless refuses to die."

"I see dead pixels!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top