This is a good little script that checks a date for you to see if it exists. It may not sound all that useful now, but how many times have you run that little ditty "30 days hath September, April, June...blah, blah, blah." This will help figure it -- and without all that singing.

Date Verification


Enter a date ('mm/dd/yy')

 

 

Source:

function checkdate(){                    //function name is checkdate

// window.onerror=null // for all other strange errors

var err=0

var psj=0;

a=document.frm.dat.value             //formname is frm; name of input type is dat; 

if (a.length != 8) err=1                //string.length is 8 characters. if more err=1

b = a.substring(0, 2)// month

c = a.substring(2, 3)// '/'

d = a.substring(3, 5)// day

e = a.substring(5, 6)// '/'

f = a.substring(6, 8)// year

//basic error checking

if (b<1 || b>12) err = 1          //if month date less than 1 or more than 12, alert "wrong input" 

if (c != '/') err = 1                 //if there is no / typed then alert "wrong input" 

if (d<1 || d>31) err = 1        // if the day number is less than 1 or more than 31, alert "wrong input"

if (e != '/') err = 1                //means the same like in variable c

if (f<0 || f>99) err = 1        //if year number less than 0 or more than 99, alert "wrong input"

 

//advanced error checking

// months with 30 days

if (b==4 || b==6 || b==9 || b==11){   //if in month april, june, september, october more than 30, then error      

if (d==31) err=1

}

// february, leap year                   //checking for the leap year, as there days in february are 29

if (b==2){

// feb

var g=parseInt(f/4)

if (isNaN(g)) {

err=1

}

if (d>29) err=1

if (d==29 && ((f/4)!=parseInt(f/4))) err=1    //not sure how this is counted lol

}

if (err==1){

alert('Wrong input!');        //if variable err tells about 1 then the alert "wrong input" shows up

}

else{                               //otherhand all is o.k

alert('OK!');

}

}

//-->

This is in body:

<form name="frm">

<input type=text name=dat value="09/11/71">

<input type=button value="Check it!" onclick="checkdate()" name="chk">

</form>

 

 

back