View Full Version : Date Validation

03-09-2005, 09:50 PM
I am wanting to validate a date field where the user must enter a date that is either equal to the current date or greather than the current date. It must not be prior to the current date. The form is coded in ASP and I have a current date field that is populated automatically using the following code:
value="<%= Date() %>". Can I insert that value into my JavaScript as a variable and then simply compare it with the value entered into the other date field.

Any help would be greatly appreciated!

03-09-2005, 11:35 PM
Yes, if its in a text box in a form.

You would simply set a variable to that form elements value.

<script type="text/javascript">

var oldDate = document.form.olddate.value;
var nowDate = document.form.newdate.value;
if (oldDate < nowDate) {
alert("date is before todays date");
} else {
alert("date is more than, or equal");}

03-10-2005, 04:39 AM
<script type="text/javascript">
function validate(oForm){
//validate date
var defaultDate = new Date(oForm.date.defaultValue); //initial value
var newDate = new Date(oForm.date.value); //new value
if (isNaN(newDate)){
alert("Invalid date.");
return false;
if ((newDate.getMilliseconds() - defaultDate.getMilliseconds()) < 0){
alert("Date must not be earlier than the current date.");
return false;
//other validations here...
return true;
<form name="form1" action="page2.asp" method="post" onsubmit="return validate(this)">
<input type="text" name="date" value="<%= Date() %>" />
<input type="submit" value="Submit" />