Javascript / Time / Date / Date Validation
Snippet details
ID: 18
Viewed: 1476
Added: 2001-11-23
Version:
Sorry no demo
makes sure some one typed in a valid date in the field
General Details
Snippet uploaded by: snippet
Email : webmaster@snippetlibrary.com
Snippet By: Unknown
<!---Head--->
<SCRIPT LANGUAGE="JavaScript">
function y2k(number) {
return (number < 1000) ? number + 1900 : number;
}
var reason = '';
function isValidDate (myDate,sep) {
// checks if date passed is in valid dd/mm/yyyy format
if (myDate.length == 10) {
if (myDate.substring(2,3) == sep && myDate.substring(5,6) == sep) {
var date = myDate.substring(0,2);
var Month = myDate.substring(3,5);
var Year = myDate.substring(6,10);
var test = New Date(Year,Month-1,date);
if (Year == y2k(test.getYear()) && (Month-1 == test.getMonth()) && (date == test.getDate())) {
reason = '';
return True;
}
else {
reason = 'valid Format but an invalid date';
return False;
}
}
else {
reason = 'invalid spearators';
return False;
}
}
else {
reason = 'invalid length';
return False;
}
}
function tellMeIfInvalid(myDate) {
if (isValidDate(myDate,'/'))
document.write(myDate + ' = valid date<BR>');
else
document.write(myDate + ' = ' + reason + '<BR>');
}
tellMeIfInvalid('21/02/1999');
tellMeIfInvalid('2190291999');
tellMeIfInvalid('2/02/1999');
tellMeIfInvalid('21/2/1999');
tellMeIfInvalid('21/02/199');
tellMeIfInvalid('32/02/1999');
tellMeIfInvalid('21/02/1999');
//-->
</SCRIPT>
<!---Body--->
none
No Reviews to show
