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 gkittelson 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.

rr236

IS-IT--Management
Oct 23, 2000
37
GB
Does anybody know how to get javascript to validate a date entry. eg. that 31 February 2001 is invalid.


 
function checkDate(date)
{
var d = new Date(date);
var mm = d.getMonth()+1;
var dd = d.getDate();
var yy = d.getYear();
mm = (mm<10)?&quot;0&quot;+mm:mm;
dd = (dd<10)?&quot;0&quot;+dd:dd;
var va = mm+&quot;/&quot;+dd+&quot;/&quot;+yy;
if(date == va)
{
alert(&quot;good date&quot;);
return true;
}
alert(&quot;invalid date&quot;);
return false;
}

checkDate(&quot;02/01/2000&quot;); adam@aauser.com
 
Try this ...

if (year2Check % 4 == 0 &amp;&amp; (year2Check % 100 != 0 || year2Check % 400 == 0)) {
month[2] = &quot;29&quot; }
else {
month[2] = &quot;28&quot; }
 
that won't tell you that, for example 01/45/2000, isn't valid, but, my example will. adam@aauser.com
 
I've finished testing lucid's code, and can confirm it works like a dream.

thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top