Home > Software design >  set an age limit in a form in react js?
set an age limit in a form in react js?

Time:11-17

The user cannot be less than 0 years old, and more than 100 years old

//format birthday date req.validated.birthdate = moment(req.validated.birthdate, "DD/MM/YYYY").format("YYYY-MM-DD");

CodePudding user response:

You can use moment().diff

const ageInYears = moment().diff(moment('15/11/1993', "DD/MM/YYYY"), 'years');

if (ageInYears > 100) {
  alert('More than 100 years old')
} else if (ageInYears < 0) {
  alert('Less than 0 years old')
} else {
  alert('Age is fine')
}

Edit: Better code

  • Related