admin管理员组

文章数量:1430078

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 plete 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 ments they do not end up being accurate long term, how do most professional sites do this correctly?

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 plete 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 ments they do not end up being accurate long term, how do most professional sites do this correctly?

Share Improve this question edited May 23, 2017 at 11:47 CommunityBot 11 silver badge asked Jun 28, 2015 at 14:50 thechrishaddadthechrishaddad 6,7458 gold badges29 silver badges34 bronze badges 1
  • 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. stackoverflow./a/21284895/1370442 – Luke Baughan Commented Jun 28, 2015 at 14:56
Add a ment  | 

2 Answers 2

Reset to default 4

I agree with the ment (but could not ment 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
    }
}

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');
}

本文标签: javascriptValidating Age from the Date of BirthStack Overflow