How can I restrict the user to enter "MM/YYYY" (example 10/1999) format only in a text box.
Created Oct 30, 2001
Nitesh Naveen
function checkDateFormat(dateValue)
{
dates = dateValue.split("/");
if(dates.length!=2)
return "1";
if(dates[0].length!=2 || dates[1].length!=4)
return "1";
if(isNan(dates[0]) || isNan(dates[1]))
return "2";
return "0";
}
function validateThis(obj)
{
if(checkDateFormat(obj.value)=="1")
{
alert('Please enter the date in the format MM/YYYY');
return false;
}
if(checkDateFormat(obj.value)=="2")
{
alert('Please enter a valid date');
return false;
}
if(checkDateFormat(obj.value)=="0")
return true;
}
Then use:
<input type=text onblur="if(!validateThis(this)) { this.focus(); }">