Birthday Script Error
I have the following script in a form, but I haven't dealt with JavaScript in a LONG time, and I'm stuck! The process I want to happen is for the user to enter their birth date, and then the form displays a message indicating their age, then the field is validated and they move to the next field. I want the field to accept only "mm/dd/yyyy", but the user is allowed to enter just about anything they want, and the field accepts the entry. There is a simple error in the script causing the field not to catch incorrectly formatted field entries. What am I missing?
//Document Level Script:
function getAge(birthDate) {
var today = new Date();
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
//Custom Format Script:
if (!event.value) {
event.value = "mm/dd/yyyy";
event.target.display = display.noPrint
}
else {
event.target.display = display.visible;
}
//Validation Script:
if(event.value != ""){
event.target.required = false;
}
else {
event.target.required = true;
}
if (event.value != "") {
var now = new Date(); // Todays Date
now.setHours(0);
now.setMinutes(0);
now.setSeconds(0);
now.setMilliseconds(0);
var d = util.scand("mm/dd/yyyy", event.value); // Date of Birth entry by user
var age = getAge(d);
if (d == null) {
app.alert("Invalid date entry! Please enter your 'Date of Birth' in 'mm/dd/yyyy' format");
event.rc = false;
}
else if (age < 18) {
app.alert("The date you entered indicates that you are less than 18 years of age!\n\nWe cannot process your application unless you are 18 years of age or older!", 1, 0)
event.rc = false;
}
else {
app.alert("You have indicated that you are " + age + " years old.\n\nIf this is correct, please move to the next question.\n\nIf it is incorrect, check your date of birth and re-enter it as necessary.", 3, 0)
event.rc = true; // this is redundant
event.value = util.printd("mm/dd/yyyy", d); // strict format
}
}
event.target.required = (event.value == "" || !event.rc);
