Check Age Javascript function

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}/;
     if(!objField.value.match(regexp)){
          alert('Invalid date - format : MM/DD/YYYY');
          return;
     }
     
     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){
          age--;    
     }else if(d.getMonth()/1==month && d.getDay()/1<=day){
          age--;    
     }
      if (age < 18)
      // if under 18 display message
      {
            alert("You must be at least 18 years of age.");
            objField.focus();
            objField.select();
            return false;
      }
      return true;
}

Thanks
lotterygirlAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

UnexplainedWaysCommented:
This might help:


var today = new Date();
var myDate = new Date();
myDate.setFullYear(1988,1,2);


// 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
alert(Math.floor(difference_ms/ONE_YEAR));





(1988,1,2);  = Year,Month,Day (and month is 0-11)
0
dakydCommented:
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);
  eighteenYearsAgo.setHours(0);
  eighteenYearsAgo.setMinutes(0);
  eighteenYearsAgo.setSeconds(1);

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

  return true;
}
</script>
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lotterygirlAuthor Commented:
Thanks dakyd.  That worked perfectly.
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.