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


cangolfer
12-20-2002, 01:22 PM
This is simple ... I can't seem to find my script anywhere.

I need to validate a date where the mask is mm/dd/yyyy.

Please help.

thanks.

swon
12-20-2002, 02:17 PM
Here is a script:

<html>
<head>
<title>Date</title>
<meta name="author" content="Swon">
<script language="JavaScript">
<!--
function datum(){

var jetzt = new Date();
var t = jetzt.getDate();
var m = jetzt.getMonth();
var j = jetzt.getYear();
var h = jetzt.getHours();
var mi = jetzt.getMinutes();
var dat = (t + "." + m + "." + j)

document.date.mydate.value=dat;// gives the hidden field named mydate the value
document.date.showdate.value=dat;//and this one only to proof the value you can delete this line and the showdate field below;

//if you want not all of it, change var dat string above;
}
//-->
</script>
<noscript></noscript>
</head>
<body text="#000000" bgcolor="#FFFFFF" link="#FF0000" alink="#FF0000" vlink="#FF0000"onLoad="datum();">
<form name="date">
<input type="hidden" name="mydate" value="">
<input type="text" name="showdate" value="">
</form>
</body>
</html>

cangolfer
12-20-2002, 02:28 PM
That is not really what I want.

If a user inputs a date in a text field, I want an onclick event that validates it is a real date.

swon
12-20-2002, 03:09 PM
It's just an easy script, maybe it is that what you want:

<html>
<head>
<title>Date</title>
<meta name="author" content="Swon">
<script language="JavaScript">
<!--
function CheckDate(x){

str = x.Date.value;
str = str.substring();

if(str.indexOf(".")== 2 && str.length == 10){
str = str.split(".");
if(str[0] < 32){days = true;}else{days=false;}
if(str[1] < 13){months = true;}else{months = false;}
if(str[2].length < 5){ years = true;}else{years= false;}

if(days && months && years)
{
alert("OK")
return true;
}
else{
alert("NOT OK");
return false;
}
}
else{
alert("Not a valid Date");
return false;
}

}
//-->
</script>
</head>
<body>
<form name="date" onSubmit="return CheckDate(this)">
<input type="text" name="Date" value="">
<input type="Submit" value="Checkdate">
</form>
</body>
</html>

cangolfer
12-20-2002, 03:51 PM
That is better, but it doesn't take into account the proper number of days for each month (30 for april, june, september, november - 31 for january, march, may, july, august, october, december - 28 for february except during leap years) nor leap years.

Does anyone have this already done?

jarry
10-04-2006, 03:11 PM
This site could be interesting.
http://www.wiseblocks.com/input-components/edit-mask-ICTextMask.html

Charles
10-04-2006, 03:31 PM
Here's what you need:<script type="text/javascript">

Date.prototype.toDateString = function () {return isNaN (this.getTime()) ? 'NaN' : [this.getMonth() < 9 ? '0' + (this.getMonth() + 1) : this.getMonth() + 1, this.getDate() < 10 ? '0' + this.getDate() : this.getDate(), this.getFullYear()].join ('/')}

</script>

<label>Date<input onchange="this.value = new Date (this.value).toDateString()"></label>

BrainDonor
10-04-2006, 03:33 PM
Here's what you need:<script type="text/javascript">

Date.prototype.toDateString = function () {return isNaN (this.getTime()) ? 'NaN' : [this.getMonth() < 9 ? '0' + (this.getMonth() + 1) : this.getMonth() + 1, this.getDate() < 10 ? '0' + this.getDate() : this.getDate(), this.getFullYear()].join ('/')}

</script>

<label>Date<input onchange="this.value = new Date (this.value).toDateString()"></label>

it's a 4 year old thread. ;)

Charles
10-04-2006, 03:40 PM
it's a 4 year old thread. ;)Good Lord, you're right. How did this thing get dug back up?