javascript regex validate years in range

后端 未结 8 1111
猫巷女王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:18

    Regex:

    /^(19[5-9]\d|20[0-4]\d|2050)$/
    

    Easier...

    var year = parseInt(textField.value, 10);
    if( year >= 1950 && year <= 2050 ) {
        ...
    }
    

提交回复
热议问题