Click to See Complete Forum and Search --> : Date validation


Jkid
01-21-2003, 08:35 AM
Hi Everybody,
I am a newbie to Javascript.

I have a text box where the user would enter the date in MM/DD/YYYY format. When the user enters the date first of all I should be able to check whether it is a valid date and then I should be able to compare that date with todays date and determine whether the date is past or a future date. can any body help me please.


thanks in advance.

JKid

Dan Drillich
01-21-2003, 12:50 PM
Please have a look at -
http://www.smartwebby.com/DHTML/date_validation.asp

Cheers,
Dan

Charles
01-21-2003, 02:33 PM
You can do much the same with a whole lot fewer lines.

<!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">
<script type="text/javascript">
<!--
Date.prototype.toString = function () {
if (isNaN(this.getTime())) {return void 0};
mm = this.getMonth() + 1;
if (mm < 10) {mm = '0' + mm};
dd = this.getDate();
if (dd < 10) {dd = '0' + dd};
yyyy = this.getFullYear();
return [mm, dd, yyyy].join('/')
}
// -->
</script>
<form action="test.html" onsubmit="if (this.date.value != new Date (this.date.value).toString()) {alert('Please enter a valid date in mm/dd/yyyy format.'); this.date.value=''; return false}">
<div><input type="text" name="date"></div>
<div><input type="submit"></div>
</form>