【问题标题】:set an age limit in a form in react js?在 react js 的表单中设置年龄限制?
【发布时间】:2021-12-26 14:42:33
【问题描述】:

用户年龄不能小于0岁,不能大于100岁

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

【问题讨论】:

    标签: javascript html node.js reactjs if-statement


    【解决方案1】:

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

    编辑:更好的代码

    【讨论】:

    • 它在没有“年”的情况下对我有用。感谢您的回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-20
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    • 2013-03-23
    • 2012-03-21
    相关资源
    最近更新 更多