<script language="javascript">
function reformatAndValidate(field) {
var str = field.value;
if (str=="") { return true; } // empty string is ok
var parts = str.replace(/\/|\-|\:/g, " ").split(" "); // change - or / or : to spaces
if (parts.length<3) { // not enough parts
field.value=str +' is invalid';
return false;
}
var year = (parts[2].length==4)?parts[2]:parseInt(parts[2],10)+2000;
var month = parseInt(parts[0],10); // change to parts[1]; if d/m/y
var date = parseInt(parts[1],10); // change to parts[0]; if d/m/y
var testDate = new Date(year, month-1, date);
if (testDate.getDate() != date || testDate.getMonth() != (month-1)) {
field.value=str +' is invalid';
return false
}
if (month<10) month = "0"+month;
if (date<10) date = "0"+date;
field.value = ""+month+"/"+date+"/"+year;
return true;
}
</script>
|