Check Age Javascript function

Posted on 2006-03-28
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

    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

    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

    Thanks dakyd.  That worked perfectly.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
    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…
    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…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now