Click to See Complete Forum and Search --> : date verification
yaron
10-13-2003, 11:42 AM
Hello all,
I want to do a date verification on onem of my form's fields.
The user must enter a date in this format : yyyy-mm-dd
The function should check if it's not a valid date (format or date)
any ideas?
Charles
10-13-2003, 12:21 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])}
Date.prototype.toDateString = function () {return [this.getFullYear(), this.getMonth() < 9 ? '0' + (this.getMonth() + 1) : this.getMonth() + 1, this.getDate() < 10 ? '0' + this.getDate() : this.getDate()].join('-')}
// -->
</script>
<form action="">
<div>
<label>Date in "yyyy-mm-dd" format.<input type="text" onchange="if(this.value != Date.fromW3CDTF(this.value).toDateString()) {alert('That would appeaar to be an invalid date or format.'); this.value=''; this.focus()}"></label>
<button type="submit">Submit</button>
</div>
</form>
schmidty
11-30-2005, 09:55 AM
is there a way to verify that the user has not enter a date in the future?
following the Charles way of coding, yes:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<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])}
Date.prototype.toDateString = function () {return [this.getFullYear(), this.getMonth() < 9 ? '0' + (this.getMonth() + 1) : this.getMonth() + 1, this.getDate() < 10 ? '0' + this.getDate() : this.getDate()].join('-')}
Date.prototype.toFutureValid =function(){
var today =new Date();
return (this<=today)?false:true
}
// -->
</script>
</head>
<body>
<form action="">
<div>
<label>Date in "yyyy-mm-dd" format.<input type="text" onchange="if(this.value != Date.fromW3CDTF(this.value).toDateString()) {alert('That would appeaar to be an invalid date or format.'); this.value=''; this.focus()};if(Date.fromW3CDTF(this.value).toFutureValid()){alert('That would appeaar to be a date in the future.'); this.value=''; this.focus()}"></label>
<button type="submit">Submit</button>
</div>
</form>
</body>
</html>