2

I am looking for a solution to validate a date of birth field which is a text box I have using jQuery Masked Input to produce the format dd/mm/yyyy, and using the dateITA:true additional methods to validate that date format.

Now what I need is a solution that validates someones age is 18 or over to complete the form, the reason I am raising this question is because most solutions I see online apparently say these validations work but will eventually be off due to leap year's etc.

I am looking for a solution that validates age down to the day, not just the year or month, basically from today to exactly 18 years ago, as this is a very important feature required for my form.

Any suggestions or articles that could be linked would be much appreciated please,

I have already seen one on here such as the following:

Calculate age in JavaScript

But according to comments they do not end up being accurate long term, how do most professional sites do this correctly?

Community
  • 1
  • 1
thechrishaddad
  • 6,523
  • 7
  • 27
  • 33
  • 1
    Id use momentJS, get the date today, subtract 18 years (using momentJS subtract) then us momentJS diff against the date they put in. This answer and links on this answer should get you there. http://stackoverflow.com/a/21284895/1370442 – Luke Baughan Jun 28 '15 at 14:56

2 Answers2

4

I agree with the comment (but could not comment myself)

function validate(date){
    var eightYearsAgo = momment().subtract("years", 18);
    var birthday = moment(date);
    
    if(!birthday.isValid()){
        // INVALID DATE
    }else if (eightYearsAgo.isAfter(birthday)){
        // 18+
    }else{
    // < 18
    }
}
Tiago Martins Peres
  • 14,289
  • 18
  • 86
  • 145
Jason H
  • 4,996
  • 6
  • 27
  • 49
  • 1
    I know you said you want it down to the 'day' but trust me, MomentJS will do it down to the day PLUS it does take into account leap years. – Jason H Jun 28 '15 at 15:47
  • This works great except for one thing, only issue I have now is, it is validating the date as MM/DD/YYYY and I have set the .format('L') to DD/MM/YYYY and locale('en-AU') but no luck? what could be possibly happening? – thechrishaddad Jun 28 '15 at 23:25
  • Dont take this the wrong way but try reading the documents at [MomentJS.com](http://www.momentjs.com). Anyways, you can change the moment to have a formatter, var birthday = moment(date, "MM/DD/YYYY"). This will put the date into the correct containers within the moment object so that it can do its logic correctly. – Jason H Jun 28 '15 at 23:34
  • Your locale is ONLY important if you are using TimeZones, which [Moment TimeZone](http://momentjs.com/timezone/) supports – Jason H Jun 28 '15 at 23:36
  • Fixed, added the format like you suggested, thank you :) it is validating it down to the day just as I wanted it to. – thechrishaddad Jun 28 '15 at 23:47
  • The only issue I have now is I need the current date to come from the actual Australia time zone not a local PC, because PC date could potentially be incorrect and the validation will be thrown off – thechrishaddad Jun 29 '15 at 00:06
  • remove one 'm' from this line var eightYearsAgo = moment().subtract("years", 18); – Teshie Ethiopia Sep 01 '22 at 13:35
0

You can use moment.js lib and calculate the difference of birth date and the currently date

const date = '16/05/2001';   // Your date
const format = 'DD/MM/YYYY'; // Your date format
const resultFormat = 'years' // Result format (years, months, days)

const age = moment().diff(moment(date, format), resultFormat, true);

if (age >= 18){
    console.log('Do what u want');
} else {
    console.log('Come back later');
}
Jonas Braga
  • 379
  • 4
  • 5