Revise: My question was not clear. If I do not enter a date of birth in the required field, it displays 'Welcome' when it should only say that if the age indicated by the DOB is over 17. Additionally, after displaying 'Welcome', it prompts me to fill out the required DOB field. Why does it continue to show welcome without an entered DOB?
Below is the relevant code snippet:
Date of Birth Field DOB:
<input type="text" name="Date" id="dateof" placeholder="YYYY-MM-DD" title="Enter your date of birth in the form YYYY-MM-DD" required /><br/><br/>
Date of Birth Validation Function
var minAge = 17;
var today = new Date()
function calcAge(birthDate, todaysDate) {
todaysDate = new Date(today);
var givenDOB = document.getElementById('dateof').value; /*date of birth entered*/
var birthDate = new Date(givenDOB);
var years = (todaysDate.getFullYear() - birthDate.getFullYear());
if (todaysDate.getMonth() < birthDate.getMonth() ||
todaysDate.getMonth() == birthDate.getMonth() && todaysDate.getDate() < birthDate.getDate()) {
years--;
}
return years;
}
function setAge() {
var age = calcAge();
if (age < minAge) {
alert("Sorry, the minimum age is 17!");
} else
alert("Welcome!");
}