javascript regex validate years in range

后端 未结 8 1110
猫巷女王i
猫巷女王i 2021-02-13 20:11

I have input field for year and I need a regex for validation it. I have such code: ^([12]\\d)?(\\d\\d)$. But I want allow to validate only years in certain range (

8条回答
  •  滥情空心
    2021-02-13 20:19

    if you want to check for age between for example 18 or 70 I did it like this. my solution was

    function yearRange(year) {
      let now = new Date().getFullYear();
      let age = now - Number(year);
      if (age < 18 || age > 70) return false;
      return true;
    }
    

提交回复
热议问题