03. oktober 2000 - 12:36
#1
<html>
<head>
<title>Validate Date</title>
<script language=\"JavaScript\">
<!--
function validatedate(f) {
if (f.dato.value==\"\") {
alert(\"Du skal skrive en dato\");
return false;
}
if (f.dato.value.length!=8) {
alert(\"Datoen skal skrives på formen MM-DD-ÅÅ\");
return false;
}
if (f.dato.value.charAt(2)!=\"-\" || f.dato.value.charAt(5)!=\"-\") {
alert(\"Datoen skal skrives på formen MM-DD-ÅÅ\");
return false;
}
var mm=parseInt(f.dato.value.substring(0,2));
var dd=parseInt(f.dato.value.substring(3,5));
var yy=parseInt(f.dato.value.substring(6,8));
if (mm<1 || mm>12 || dd<1 || dd>31) {
alert(\"Ulovlig dato\");
return false;
}
if (yy>=70) { yyyy=yy+1900 } else { yyyy=yy+2000; }
d=new Date(yyyy,mm-1,dd,0,0,0,0);
if (d.getFullYear()!=yyyy || (d.getMonth()+1)!=mm || d.getDate()!=dd) {
alert(\"Ulovlig dato2\");
return false;
}
return true;
}
//-->
</script></head>
<body>
<form method=post action=\"\" name=\"eksform\" onsubmit=\"return validatedate(document.eksform)\">
<input type=\"text\" name=\"dato\"><br>
<input type=\"submit\">
</form>
</body>
</html>
03. oktober 2000 - 12:39
#2
Lidt rodet men...
<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">
<html>
<head>
<title>isDateValid(d, m, y) v1.0</title>
<script type=\"text/javascript\" language=\"JavaScript\">
function getFirstDayOfMonth(inMonth, inYear) {
tDate = new Date(inYear, (inMonth-1), 1, 10,0,0,0)
d = tDate.getDay();
if(d == 0) { d = 6; }
else { d--; }
return (d+1);
}
function getMonthLength(inMonth, inYear) {
// 1 - 12 not lousy 0-11!! - to Kewl to be true ;) - Fini
tDate2 = new Date(inYear, (inMonth-1), 1, 10,0,0,0)
if(inMonth != 2) {
tDate2.setDate(31)
if(tDate2.getDate() == 31) {
return 31
}
else {
tDate2.setDate(30)
if(tDate2.getDate() == 30) {
return 30
}
}
}
else {
// februrary same but with 29,28
tDate2.setDate(29)
if(tDate2.getDate() == 29) {
return 29
}
else {
tDate2.setDate(28)
if(tDate2.getDate() == 28) {
return 28
}
}
}
return false;
}
function isDateValid(inDay, inMonth, inYear) {
// Name: isDateValid() v1.0 by Fini A. Alring [alring@email.com] (c) 2000 - Framfab Networkers A/S
// Purpose: to validate a given date.... returns true if the date actually existed or will exist...
if( (inMonth <= 12) && (inMonth >= 1) && (inDay <= 31) && (inDay >= 1)) {
if(getMonthLength(inMonth, inYear) < inDay) {
// alert(\'not valid, date does not exist...\')
}
else {
return true;
}
}
else {
// alert(\'isDateValid() : Not valid, out of bounds...d: 1-31, m: 1-12 y: -????-????\')
}
return false;
}
//alert(isDateValid(32,12,2000))
</script>
</head>
<body>
<h1>isDateValid(d, m, y) v1.0<h3>by Fini A. Alring [alring@email.com]<br>(c) 2000 - Framfab Networkers A/S<h3>
<form name=\"tForm\">
<input type=\"text\" size=\"4\" name=\"tDay\" value=\"31\">
<input type=\"text\" size=\"4\" name=\"tMonth\" value=\"12\">
<input type=\"text\" size=\"4\" name=\"tYear\" value=\"2000\">
<input type=\"button\" size=\"4\" name=\"testBut\" value=\"isDateValid(d, m, y)\" onclick=\"alert(isDateValid(document.tForm.tDay.value, document.tForm.tMonth.value, document.tForm.tYear.value))\">
</form>
</body>
</html>
/Geeeet