0

I am developing a system which requires that you should be at least 18 years old to register.

For doing this validation i have implemented date difference in javascript in following way, but it is not accurate, is there any javascript function or other way to do this?

var d1=new Date(1985,1,28);
var d2=new Date();
var milli=d2-d1;
var milliPerYear=1000*60*60*24*365.26;
var years_old=milli/milliPerYear; 
2
  • 2
    Any client side validation can easily be circumvented. Always validate on the server side. Although in this case, you cannot even ensure that a person provides his real birthday. Commented Jan 28, 2012 at 11:03
  • Yeah - this is more of a user-consent liability issue, rather than a full credit check or anything. Client-side validation is fine. Commented Nov 28, 2014 at 14:24

2 Answers 2

8

Legally being at least 18 years old is not about the amount of time corresponding to the average duration of 18 years (years aren't always the same length). It is about the current date being after your 18th birth date. Hence, you should just add 18 to the year count on the birthdate and see if this is before or after the present date, e.g.

var birthDate = new Date(1985,1,28);
var today = new Date();
if (today >= new Date(birthDate.getFullYear() + 18, birthDate.getMonth(), birthDate.getDate())) {
  // Allow access
} else {
  // Deny access
}

You should do the same validation on the server side as well.

Note that this also handles people born on 29th February the correct way: in this case JavaScript will create a date object to represent the 1st March 18 years later.

Sign up to request clarification or add additional context in comments.

8 Comments

Important point. We don't celebrate our birthdays by counting the milliseconds since we were born.
@mack Yes, why would you expect otherwise?
sorry, var birthDate = new Date(1994,1,27); var today = new Date(); if (today >= new Date(birthDate.getFullYear() + 18, birthDate.getMonth(), birthDate.getDay())) { alert("Allow"); } else { alert("Deny"); } try this, it alerts deny
@mack That's right. I mistyped the name of day-of-month accessor: it was getDay() while it should be getDate(). Corrected now. Thanks!
var birthDate = new Date(1994,1,27); var today = new Date(); if (today >= new Date(birthDate.getFullYear() + 18, birthDate.getMonth(), birthDate.getDate())) { alert("Allow"); } else { alert("Deny"); } still alert deny
|
1

I like http://momentjs.com/

<script src="moment.min.js"></script>
<script>
  var dob = new moment([1985,1,28]);
  var age = new.moment().diff(dob, 'years')

  if (age >= 18) { 
    ...
  }
</script>

1 Comment

var dob = new moment([1985,1,30]); var age = new moment().diff(dob, 'years') alert(age); it alerts 27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.