I wonder if anyone has worked out some code to work out an age based on Eg.date of birth.
Two text fields.One for Date of Birth.One for the Age in years.
I have tried this script:
JAVA
function ageCount() {
var date1 = new Date();
var dob= document.getElementById("date_of_birth").value;
var date2=new Date(dob);
var pattern = /^\d{1,2}\/\d{1,2}\/\d{4}$/; //Regex to validate date format (dd/mm/yyyy)
if (pattern.test(dob)) {
var y1 = date1.getFullYear(); //getting current year
var y2 = date2.getFullYear(); //getting dob year
var age = y1 - y2; //calculating age
birth.value=(age) + 2;
return true;
} else {
alert("Invalid date format. Please Input in (dd/mm/yyyy) format!");
return false;
}
}
HTML
<input type="text" name="date_of_birth" id="date_of_birth" />
<input type="submit" value="Age" onClick="ageCount();">
But still cannot get it to put the age into a text field.
I hope it is OK to ask such questions in this forum.