...

View Full Version : Need Help taking Date from form field and checking if over 18 years old



Broll05
12-08-2010, 02:58 AM
I am having some trouble with my code in that I am trying to take the value from the date of birth field and check to see if over 18 years old. I they are not over 18, a window.alert dialog box pops up to tell them. Any help with this I would greatly appreciate.

Below is the check if 18 validation.

Script section in the document head:


//check if over 18

function overAge(){
var age;
var input = document.forms[0].birthDate.value;
var pyear = parseInt(input.substring(6,10));
var pmonth = parseInt(input.substring(0,2)) - 1;
var pday = parseInt(input.substring(3,5));

if ( month < pmonth ){
age = year - pyear - 1;
}
else if ( month > pmonth ){
age = year - pyear;
}
else if ( month == pmonth ){
if ( day < pday ){
age = year - pyear - 1;
}
else if ( day > pday ){
age = year - pyear;
}
else if ( day == pday ){
age = year - pyear;
}
}
if(age < 18){
window.alert('Attention: Under 18!');
valid=false;
return valid;
}
}


Code for body section:


Date of Birth: <br/>
<input type = "text" name = "date" value = "(mm/dd/yyyy)"/>

jmrker
12-08-2010, 03:42 AM
Can you use this instead?


<html>
<head>
<title>Age Check</title>
<script type="text/javascript">
// For: http://www.codingforums.com/showthread.php?t=211373

function checkAge(ageInfo) {
var today = new Date();
var tmp = ageInfo.split('/');
if ((today.getFullYear() - tmp[2]) < 18) { alert('Attention: Under 18'); return false}
return true;
}
</script>
</head>
<body>
Date of Birth: <br/>
<input type="text" name="date" value="mm/dd/yyyy" onblur="checkAge(this.value)" />
</body>
</html>

Philip M
12-08-2010, 09:40 AM
Can you use this instead?


That simply checks the year but if the 18th birthday (say 12/31/1992) has not been reached it still evaluates to true.


<script type="text/javascript">

function checkAge(bmth,bdy,byr){

var today = new Date();
var nowyear = today.getFullYear();
bmth = bmth - 1; // in JavaScript months are 0-11

var age = nowyear - byr;
var nowmonth = today.getMonth(); // months are 0-11
var nowday = today.getDate();
if (bmth > nowmonth) {age = age - 1} // next birthday not yet reached
else if (bmth == nowmonth && nowday < bdy) {age = age - 1} // next birthday not yet reached
alert('You are ' + age + ' years old');
}

checkAge(12,31,1992); // MM/DD/YYYY format
</script>



"Insanity is doing the same thing over and over again, but expecting different results." - Rita Mae Brown

Philip M
12-08-2010, 10:17 AM
If you want a more extended script which also checks that the entered date is valid (not 43rd February 2020):-


<script type="text/javascript">

var valid = false;

function checkAge(bday){
var today = new Date();
var nowyear = today.getFullYear();

var bdate = bday.replace(/-/g,"/");
d = bdate.split("/");
var byr = parseInt(d[2]);
if (byr <1900) {byr = byr + 2000}
var bmth = parseInt(d[1],10); // radix 10!
var bdy = parseInt(d[0],10); // radix 10!

checkValidDate(byr,bmth,bdy);
if (!valid) {return false}

var age = nowyear - byr;
var nowmonth = today.getMonth(); // months are 0-11
var nowday = today.getDate();
if (bmth > nowmonth) {age = age - 1} // next birthday not yet reached
else if (bmth == nowmonth && nowday < bdy) {age = age - 1} // next birthday not yet reached
alert('You are ' + age + ' years old');
}

function checkValidDate(yr,mmx,dd) {

var today = new Date();
var nowyear = today.getFullYear();
if (yr <1910 || yr > nowyear-1) { // you may want to change this to some other year!
alert ("Impossible Year Of Birth!")
return false;
}

mm = mmx-1; // remember that in Javascript date objects the months are 0-11
var nd = new Date();
nd.setFullYear(yr,mm,dd); // format YYYY,MM(0-11),DD

var ndmm = nd.getMonth();
if (ndmm != mm) {
alert (dd + "/" + mmx + "/" + yr + " is an Invalid Date!");
valid = false;
return valid;
}
else {
alert (dd + "/" + mmx + "/" + yr + " is a Valid Date");
valid = true;
return valid;
}
}


checkAge("03/07/1988"); // MM/DD/YYYY format
</script>

Broll05
12-08-2010, 02:30 PM
Thanks both of for your help. Your expertise is always appreciated Philip M, Thank you.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum