Click to See Complete Forum and Search --> : Date Validation..
richard76
09-09-2003, 11:18 AM
Dear All,
I have some toubts in date validation. I have two input Fields for "From Date" and "To Date" for the ticket reservation Form. I just want to validate the dates when the user key in to the text fields(on focus). It means the user must enter the Valid From Date(Must be greater than System Date) and the To Date (should be greater than "From Date"). I want to use YYYY-MM-DD(2003-09-10) format in the input boxes. Can I have the coding for this problem. Please help me.
Thanx for your concern.
Regards,
Richard.
well, take a look at http://www.yxscripts.com/fg/form.html, it can help you to
- check dates against any given format with yyyy, mm and dd as year, month and day
- check whether a date falls in some date range
- check whether two dates are good for a date range
- bunch of other form checkings
thx,
AdamBrill
09-09-2003, 01:02 PM
Or try this:<html>
<head>
<title>Untitled</title>
<script type="text/javascript">
String.prototype.change = function(){
test = this.split('-');
return(test[1]+"-"+test[2]+"-"+test[0]);
}
function validate(){
nowdate = new Date();
fromdate = new Date(document.form1.fromdate.value.change());
todate = new Date(document.form1.todate.value.change());
if(!isNaN(fromdate) && !isNaN(todate)){
if(fromdate-nowdate>0){
if(todate-fromdate>0){
alert("Validated correctly!");
}else{
alert("To Date must bo after From Date.");
}
}else{
alert("From Date must be after todays date.");
}
}else{
alert("The dates you entered are not formatted correctly.");
}
}
</script>
</head>
<body>
Format: YYYY-MM-DD<br>
<form name="form1">
From Date:<input type="text" name="fromdate"><br>
To Date:<input type="text" name="todate"><br>
<input type="button" onclick="validate()" value="Validate!">
</form>
</body>
</html>
Charles
09-09-2003, 01:15 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.fromW3CDTF = function (s) {var a = s.split('-'); return new Date(a[0], --a[1], a[2])}
// -->
</script>
<form action="">
<div>
<label>From Date<input type="text" name="fromDate" onchange="if (new Date() > Date.fromW3CDTF(this.value)) {alert('Invalid date'); this.value = ''; this.focus()}"></label>
<label>To Date<input type="text" name="toDate" onchange="if (Date.fromW3CDTF(this.form.fromDate.value) >= Date.fromW3CDTF(this.value)) {alert('Invalid date'); this.value = ''; this.focus()}"></label>
<button type="Submit">Submit</button>
</div>
</form>