I am using jQuery on a registration form and I'm have a problem with my Age Verification function. Here is the code:
function validateDOB(){
//testing regular expression
var a = $("#dob").val();
var filter = /^[0-9]{2}-[0-9]{2}-[0-9]{4}$/;
var thisDay = new Date();
var day = parseInt(thisDay.getDate());
var month = parseInt(thisDay.getMonth()) + 1;
var year = parseInt(thisDay.getFullYear());
var dobarray = a.split("-");
var dobday = parseInt(dobarray[1]);
var dobmonth = parseInt(dobarray[0]);
var dobyear = parseInt(dobarray[2]);
//if it's valid email
if(filter.test(a) && (((year - dobyear) > 17) || (((year - dobyear) == 17) && ((month - dobmonth) >= 0) && ((day - dobday) >= 0)))){
dob.removeClass("error");
dobInfo.text("(mm-dd-yyyy) This is 17+ only!");
dobInfo.removeClass("error");
return true;
}
//if it's NOT valid
else{
dob.addClass("error");
dobInfo.text("Please give us a valid birthday please... please? (mm-dd-yyyy)");
dobInfo.addClass("error");
return false;
}
}
So, basically I want a user to input their birthday in mm-dd-yyyy format and this script should theoretically check that format and also the age of the person to see if they are 17 or older; the format filter works fine, but the age calculated part doesn't. I am using a jQuery and PHP verification system, and the PHP equivalent to the above code works just fine. Knowing nearly nothing about JavaScript and jQuery, I translated what I did with PHP into JavaScript vocabulary.
I'll appreciate any critiquing you may have!