You can use the following code, & see whether it meets ur need..
function CheckDate(element)
{
//check that the value is of [0-9]
//convert all values to lower char
var msg = "Invalid value for Date: " + element.value;
var str = element.value;
str.toLowerCase;
for (var i = 0; i < str.length; i++)
{
var ch = str.substring(i, i + 1);
if ((ch >= "0" && "9" >= ch) || ch == "." )
{
}else{
alert(msg);
element.value = element.defaultValue;
element.focus();
return false;
}
}
var year = str.substring(6,10);
var month = str.substring(3,5);
var day = str.substring(0,2);
if ( "1997" > year || year > "2099" )
{
msg = "Invalid value for Year: " + element.value + " " + year;
alert(msg);
element.value = element.defaultValue;
element.focus();
return false;
}
if ( "01" > month || month > "12" )
{
msg = "Invalid value for Month: " + element.value + " " + month;
alert(msg);
element.value = element.defaultValue;
element.focus();
return false;
}
if ( "01" > day || day > "31" )
{
msg = "Invalid value for Day: " + element.value + " " + day;
alert(msg);
element.value = element.defaultValue;
element.focus();
return false;
}
if ( month == "02" && day > (((year%4==0 && year%100!=0)
|| year%400==0)?29:28))
{
msg = "Invalid value for Day(02): " + element.value + " " + day;
alert(msg);
element.value = element.defaultValue;
element.focus();
return false;
}
if ((month == "04" || month == "06" || month == "09" || month == "11")
&& day > "30")
{
msg = "Invalid value for Day(30): " + element.value + " " + day;
alert(msg);
element.value = element.defaultValue;
element.focus();
return false;
}
return true;
}
Pls check this for both IE & Netscape.
Main Topics
Browse All Topics





by: fritz_the_blankPosted on 2001-10-31 at 05:42:34ID: 6604718
Pass the name of the field (lclObject) and a message friendly name of the field (lclMessage) and this should do it:
\/(\d{4,}) [ ]*$/; x); ch[1],10)- 1,match[2] ); nt(match[2 ],10) && tmpdate.getFullYear()==par seInt(matc h[3]) && (tmpdate.getMonth()+1)==pa rseInt(mat ch[1],10))
function ch_date(lclObject, lclMessage)
{
var value =""
value = eval("document.formname." + lclObject + ".value")
if(value.length>0)
{
var dateregex=/^[ ]*[0]?(\d{1,2})\/(\d{1,2})
var match=value.match(daterege
if (match)
{
var tmpdate=new Date(match[3],parseInt(mat
if (tmpdate.getDate()==parseI
{
return false;
}
}
alert("The " + lclMessage + " must be a valid date in the format MM/DD/YYYY");
eval('document.formname.' + lclObject + ".focus()");
return true;
}
else
{
return false;
}
}