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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Validation using FAQ script

Status
Not open for further replies.

aks78

Technical User
Mar 19, 2003
3
US
I've been trying to use the Date Validation script provided in the Javascript FAQ and I'm having trouble with it using the onChange event in my date fields. It seems to always give me the alert even if it's a valid date. Here is the script:

-----------------------------------------------------------

<html>
<head>

<script language=&quot;JavaScript&quot;>
function isLeap(year){
if(year % 400 == 0){
return true;
} else if((year % 4 == 0) && (year % 100 != 0)){
return true
} else return false;
};

function days_in(month, year){
if(month == 4 || month == 6 || month == 9 || month == 11){
return 30;
} else if(!isLeap(year) && month == 2){
return 28;
} else if(isLeap(year) && month == 2){
return 29;
} else return 31;
};

function checkDate(myItem){
var myArrayDate, myDay, myMonth, myYear, myString, myYearDigit;
myString = myItem.value + &quot;&quot;;
if (myString == &quot;&quot; || myString == &quot;mm/dd/yyyy&quot;){
myItem.value = &quot;mm/dd/yyyy&quot;;
return true;
}
myArrayDate = myString.split(&quot;/&quot;);

myDay = Math.round(parseFloat(myArrayDate[1]));
myMonth = Math.round(parseFloat(myArrayDate[0]));
myYear = Math.round(parseFloat(myArrayDate[2]));
myString = myYear + &quot;&quot;;
myYearDigit = myString.length;

if (isNaN(myDay) || isNaN(myMonth) || isNaN(myYear) || (myYear < 1) || (myDay < 1) || (myMonth < 1) || (myMonth > 12) || (myYearDigit != 4) || (myDay > days_in(myMonth, myYear))){
alert(&quot;Please check your Date format. (mm/dd/yyyy)&quot;);
myItem.value = &quot;mm/dd/yyyy&quot;;
return true;
} else{
return false;
}
};
</script>
</head>
<body>
<input type=&quot;text&quot; name=&quot;myDate1&quot; onChange=&quot;checkDate(this.value)&quot;>
</body>
</html>

-----------------------------------------------------------

any ideas? thanks...
 
Hi!

Change:
myString = myItem.value + &quot;&quot;;

To:
myString = myItem + &quot;&quot;;


HTH Jessica [ponytails2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top