在react js中的表单中设置年龄限制



用户年龄不能低于0岁,年龄不能超过100岁

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

您可以使用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')
}

编辑:更好的代码

最新更新