Click to See Complete Forum and Search --> : checking for a date value
Can anyone tell me if, and if so, how I can check if a combination of dd, mm and yyyy is a valid date?
Thanx!
ShIzO
Oct 18th, 2000, 01:14 PM
<script language='JavaScript'>
function checkDate() {
var myDate = document.[formName].[textfieldname].value;
// assuming that date format is MM/DD/YYYY
var month = mydate.substring(0,2);
var day = mydate.substring(3,5);
var year = mydate.substring(6,10)
//then you perform checks
if (!((month > 0) && (month < 13))) {
alert("Month must be 1-12!");
reuturn false;
}
//
// then for day and year
}
call that function upon submiting the form or whatever
Thanx, that's very nice, but it doesn't check wether or I enter November 31st as a date, for instance. I've written the whole function myself now, and it works, but I was just wondering if maybe there was a stadard function for it, but I guess the answer to that question is now, huh?
Thanx anyway!
Oh, in case anyone is interested, here's the code I came up with (granted: it doesn't check if the year is divisable by 100 or 400, but then again, if the code is still running in 2100, I'll come and adjust it for you personally) :
BTW, please pay no intention to the lack of indentation.. for some reason it won't work for me..
-----------------------------------------------------
function IsValidDate(myday, mymonth, myyear)
{
if ((myday < 1) || (myday > 31))
{
return false;
}
if ((mymonth < 1) || (mymonth > 12))
{
return false;
}
if (myyear < 2000)
{
return false;
}
switch (mymonth)
{
case "2" :
if (!((myday <= 28) || ((myday <= 29) && ((myyear % 4) = 0))))
{
return false;
}
case "4" :
if (myday > 30)
{
return false;
}
case "6" :
if (myday > 30)
{
return false;
}
case "9" :
if (myday > 30)
{
return false;
}
case "11" :
if (myday > 30)
{
return false;
}
}
return true;
}
-----------------------------------------------------
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.