We help IT Professionals succeed at work.

Check Age Javascript function

lotterygirl
lotterygirl asked
on
Medium Priority
5,737 Views
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}/;
     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
Comment
Watch Question

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)
Commented:
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>

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thanks dakyd.  That worked perfectly.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.