Help - Search - Members - Calendar
Full Version: JS Age Verification Help
Pixel2Life Forum > Help Section > PHP, ASP, MySQL, JavaScript and other Web/Database Programming Help
Vermillio
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!
rc69
I don't have time to run through the math myself right now, but in the mean time i can recommend this: yodasearch.gif
http://www.pixel2life.com/search/age_IN_15...nytime_IN_live/

Of course, i didn't find anyhting simple in that result set, but maybe something in there will work for you.
Vermillio
It isn't that the math is wrong. Like I said in my original post, I made a PHP version of this script that works. This jQuery/JS/Ajax script does recognize the pattern filter such that my error class is added to a text field when the pattern is not correct. However, when I enter say 11-09-1992, this person is not 17 yet and going through the script, my error class should be added because 10 (October) - 11 (November) = -1 which is <0, obviously. What I am thinking is that something may be wrong in how I tried to express this concept as opposed to the concept itself. My first language is PHP and, not knowing enough of JavaScript/AJAX/jQuery Library, I basically took my technique that I used in PHP and translated it to JS.
rc69
QUOTE (Vermillio @ Oct 7 2009, 04:10 PM) *
What I am thinking is that something may be wrong in how I tried to express this concept as opposed to the concept itself.

By "Check the math" that's kind of what i meant. I'm not familiar enough with the JS date object to say off-hand whether or not what you're doing is correct (where as in PHP, i probably could have).

Have you tried debugging the script to see what values were actually returned by Date method calls and manually comparing them with what the JS took to be the entered values for the dob?

i.e.
CODE
window.alert(year + '-' + dobyear + '=' + (year - dobyear));
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.