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!
JS Age Verification Help
Started by Vermillio, Oct 06 2009 11:29 PM
3 replies to this topic
#1
Posted 06 October 2009 - 11:29 PM
#2
Posted 07 October 2009 - 10:39 AM
I don't have time to run through the math myself right now, but in the mean time i can recommend this: 
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.
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.
#3
Posted 07 October 2009 - 05:10 PM
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.
#4
Posted 07 October 2009 - 09:48 PM
Vermillio, on Oct 7 2009, 04:10 PM, said:
What I am thinking is that something may be wrong in how I tried to express this concept as opposed to the concept itself.
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.
window.alert(year + '-' + dobyear + '=' + (year - dobyear));
Edited by rc69, 07 October 2009 - 09:49 PM.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users
