My form requires Date of Birth entered in three selects as MM/DD/YYYY and there are two different age limits, depending on the type of user entered. Both are currently being validated through javascript and returning immediate errors however users can still submit with invalid dates.
I believe that I need for the dates to be validated and required by my PHP program in order to stop invalid form submissions. I'm looking for the script to accomplish that validation advice on the best way.
Here is the current javascript validation for the date of birth fields:
On my form, I have three select fields representing MM, DD, YYYY that are currently being tested for maximum age. I also need to check that the dates selected are valid, as it relates to # of days in month and leap years. Looking for a script that will work well with existing check and validate date prior to age check.
Here is current script:
<SCRIPT LANGUAGE="JAVASCRIPT">
<!-- hide this script tag's contents from old browsers
function checkAge(mode)
{
var max_age =75; //Considering its the parent
/* set the maximum age you want to allow in */
if(mode=="member" || mode=="member2") {
max_age=75;
}
if(mode=="child" || mode=="child2" || mode=="child3" || mode=="child4" || mode=="child5") {
max_age=23;
}
/* change "age_form" to whatever your form has for a name="..." */
var year = parseInt(document.forms["s
ignup"]["y
ear" + mode].value);
var month = parseInt(document.forms["s
ignup"]["m
onth" + mode].value)-1;
var day = parseInt(document.forms["s
ignup"]["d
ay" + mode].value);
var theirDate = new Date((year + max_age), month, day);
var today = new Date;
var birth = new Date(year, month, day);
var age = today.getFullYear() - birth.getFullYear();
if (theirDate < today) {
alert("Member must be under " + max_age + " years of age.");
return false;
}
else {
return true;
}
}
// -- done hiding from old browsers -->
</script>
Start Free Trial