Click to See Complete Forum and Search --> : Date evaluates incorrect


KBSJohn
10-21-2003, 03:50 PM
I have a form that I want to accept and validate a date on, then use that date to compose a message to to user. Everything works as I would expect, unless you enter a date between 08/01/1985 and 09/30/1985. If you enter a date such as 9/30/1985, it will evaluate correct..... Don't have a clue, very new to this all, just trying to get a leg up.

Here's the script

<script>
dtFormat = 'MM/DD/CCYY'

function chkDt()
{
udt = document.frm.mdt.value;
if(udt.indexOf("/") == -1){
alert('Not a valid date, format '+dtFormat);
document.frm.mdt.focus();
return false;
}
dt1 = udt.split("/")
mm1 = parseInt(dt1[0]);
dd1 = parseInt(dt1[1]);
yy1 = parseInt(dt1[2]);
if(isNaN(dd1) || isNaN(mm1) || isNaN(yy1)){
alert('Invalid Date !');
return false;
}
dt2 = new Date(mm1+'/'+dd1+'/'+yy1)
dd2 = dt2.getDate();
mm2 = dt2.getMonth()+1;
yy2 = dt2.getFullYear();

//alert(dd1+'/'+mm1+'/'+yy1);
//alert(dd2+'/'+mm2+'/'+yy2);
if(dd1==dd2 && mm1==mm2 && yy1==yy2)
{
//alert('Year '+yy2)
//if (yy2=='1985') alert('Must Play 18U');
//else
if (mm2<09) yy2=yy2-1;
else yy2=yy2;
yy2=2003-yy2;
alert('Must Be Playing '+yy2+'U');
}
else
alert('Invalid Date ! ');
document.focus();
}
</script>


and how I use it

<center>
<form name=frm>
Enter a Birth Date (MM/DD/CCYY) <input type=text name=mdt size=8 maxlength=10>
<br>
<input type=button value="Check Date" onclick="chkDt()">
<br>
</form>
</center>

Charles
10-21-2003, 04:09 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>

<style type="text/css">
<!--
label {display:block; margin:1em 0em}
input {display:block}
-->
</style>

<script type="text/javascript">
<!--
Date.prototype.toDateString = function () {return [this.getMonth() < 9 ? '0' + (this.getMonth() + 1) : this.getMonth() + 1, this.getDate() < 10 ? '0' + this.getDate() : this.getDate(), this.getFullYear()].join ('/')}
// -->
</script>

<form action="">
<div>
<label>Date<input type="text" onchange="if (this.value != new Date(this.value).toDateString()) {alert('That would not appear to be a valid date.'); this.value = ''; this.focus()}"></label>
<button type="submit">Submit</button>
</div>
</form>

KBSJohn
10-21-2003, 04:19 PM
Thanks Charles, that will do nicely. Still would like to know what the problem was with the code, somehow it wasn't parsing out the date correctly.

Thanks again