I understand the concept, but I'm not sure how to write the code to convert the date input into a Date object.
To practice, I created a simple ASP page with a form that contains one text field and a submit button. For simplicity's sake, the text field value is initially populated with today's date.
I wrote two JavaScript functions to be activated onSubmit: (1) a check for a null value in the text field, and (2) the date conversion and comparison. The first function works fine, but there's something with the 2nd function I'm missing and it's probably the conversion code.
Here's what I have so far:
- <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head>
-
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-
<title>Untitled Document</title>
-
-
<script type="text/javascript">
-
function checkform()
-
{
-
if (document.form1.entrydate01.value == "")
-
{
-
//something is wrong
-
alert('REQUIRED FIELD ERROR: Please enter date in field!')
-
return false;
-
}
-
// if script gets this far through all of your fields
-
// without problems, it's ok and you can submit the form
-
return true;
-
}
-
</script>
-
-
<script type="text/javascript">
-
function checkdate()
-
{
-
var myDate = new Date(document.form1.entrydate01.value);
-
var today = new Date();
-
{
-
if (myDate>today)
-
{
-
alert('You cannot enter a date in the future!')
-
return false;
-
}
-
else alert('This is a valid date')
-
return true;
-
}
-
}
-
</script>
-
</head>
-
<body>
-
-
<form action="" method="post" name="form1" id="form1" onsubmit="return checkform();return checkdate()">
-
<label>
-
<input type="text" name="entrydate01" id="entrydate01" value="<%= FormatDateTime(Date, 0)%>"/>
-
</label>
-
<label>
-
<input type="submit" name="Submit" id="Submit" value="Submit" />
-
</label>
-
</form>
-
-
</body>
-
</html>
Any suggestions?