View Full Version : Validate Date of Birth

Jun 24th, 2005, 02:30 PM
<input type="text" size=8 name="dob" STYLE="color: black; font: 7pt Verdana, Arial, Helvetica, sans-serif;">

I have that text box.

How do i put JS Validation on it so that if the date of birth entered checks if the DOB means they are less that two years old?

Check against todays date? Anyone got code for this? Help GREATLY appeciated.

In other words 020304 = true
010290 = false

I.e. if they are over two years old show a JS Alert.

Is this possible?


Jun 24th, 2005, 03:11 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
/* Extension to Date object by glenngv
* (http://www.codingforums.com/member.php?find=lastposter&t=62011)
Date.prototype.getDiff = function(date, interval){
if (typeof date == "string"){
date = new Date(date);
if (isNaN(date) || !(date instanceof Date)){
return NaN;
if (typeof interval == "undefined") interval = "ms";
var diff = this - date;
case "s":
diff = diff/1000; break;
case "n":
diff = diff/(1000*60); break;
case "h":
diff = diff/(1000*60*60); break;
case "d":
diff = diff/(1000*60*60*24); break;
case "m":
diff = diff/(1000*60*60*24*30); break;
case "y":
diff = diff/(1000*60*60*24*365); break;
return Math.floor(diff);

function checkBirthday(obj) {
if( !/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/.test(obj.value) ) {
alert( "Invalid date supplied - must be format DD/MM/YYYY" );

var d = new Date();
var d2 = new Date(RegExp.$3, RegExp.$2, RegExp.$1);

var diff = d.getDiff(d2, "y")
if( isNaN(diff) ) {
alert( "Invalid date supplied" );
else if( diff < 2 ) {
alert( "Difference less than 2 years" );

<form name="myForm">
<input type="text" name="birthday"><br>
<input type="button" value="check date" onclick="checkBirthday(document.myForm.birthday);">

Hope this helps