Check Age Javascript function

Posted on 2006-03-28
Medium Priority
Last Modified: 2008-02-01
I have a javascript function that checks to see if the person is over 18 according to the date of birth they entered in a text box.  My problem is that today's date is 3/28/2006 and if they enter a birthdate greater then 2/1/9888 it doesn't show them as 18.  In case I didn't say it right that means that if they enter 2/1/1988 it doesn't pop up the under 18 message.  If they enter 2/2/1988 it does pop up the under 18 message.  Anyone born on or before 3/28/1988 should pass the over 18 test.  Can you tell what I'm doing wrong or have a better suggestion on how to write this function?

function CheckAge(objField)
     var regexp = /\d{2}\/\d{2}\/\d{4}/;
          alert('Invalid date - format : MM/DD/YYYY');
     var day = objField.value.substr(3,2);
     var month = objField.value.substr(0,2);
     var year = objField.value.substr(6,4);
     var d = new Date();

     var age = d.getYear() - year;
     if(d.getMonth()/1 < month){
     }else if(d.getMonth()/1==month && d.getDay()/1<=day){
      if (age < 18)
      // if under 18 display message
            alert("You must be at least 18 years of age.");
            return false;
      return true;

Question by:lotterygirl
LVL 12

Expert Comment

ID: 16316131
This might help:

var today = new Date();
var myDate = new Date();

// The number of milliseconds in one day
var ONE_DAY = 1000 * 60 * 60 * 24
var ONE_YEAR = ONE_DAY * 365;

var date1_ms = today.getTime()
var date2_ms = myDate.getTime()

// Calculate the difference in milliseconds
 var difference_ms = Math.abs(date1_ms - date2_ms)
// Convert back to days and return

(1988,1,2);  = Year,Month,Day (and month is 0-11)
LVL 19

Accepted Solution

dakyd earned 1000 total points
ID: 16323916
Unexplained, that doesn't account for leap years.  Odds are, there will be at least one leap year involved.

Here's my stab at it, it just sets up a date of 18 years ago (1 millisecond after midnight), and then compares with that.  If objField.value is less than or equal to that date then you're 18.  Otherwise, you'll get that error message.  It gets around the leap year issue by just setting the year automatically, rather than trying to subtract off what the correct amount of time should be.  Hope that helps.

<script type="text/javascript">
function checkAge(objField)
  var dateToCheck = new Date(objField.value);
  var eighteenYearsAgo = new Date();

  eighteenYearsAgo.setYear(eighteenYearsAgo.getFullYear() - 18);

  if (dateToCheck.getTime() > eighteenYearsAgo.getTime())
    alert("You must be at least 18 years of age.");
    return false;

  return true;

Author Comment

ID: 16326773
Thanks dakyd.  That worked perfectly.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question