Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    20
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question Need Help taking Date from form field and checking if over 18 years old

    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:
    Code:
                            //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:
    Code:
    Date of Birth: <br/>
    	<input type = "text" name = "date" value = "(mm/dd/yyyy)"/>

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,077
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb Does this work for you?

    Can you use this instead?
    Code:
    <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>

  • Users who have thanked jmrker for this post:

    Broll05 (12-08-2010)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,922
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by jmrker View Post
    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.

    Code:
    <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

  • Users who have thanked Philip M for this post:

    Broll05 (12-08-2010)

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,922
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    If you want a more extended script which also checks that the entered date is valid (not 43rd February 2020):-

    Code:
    <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>
    Last edited by Philip M; 12-08-2010 at 09:28 AM.

  • Users who have thanked Philip M for this post:

    Broll05 (12-08-2010)

  • #5
    New Coder
    Join Date
    Dec 2010
    Posts
    20
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Thanks both of for your help. Your expertise is always appreciated Philip M, Thank you.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •