why is <input type=“number” maxlength=“3”> not working in Safari?

后端 未结 8 1396
刺人心
刺人心 2020-12-09 01:55

I like to have an input with maximum 3 characters. In Firefox everything works fine I can only write 3 numbers inside the input - but in safari you can write a lot of number

相关标签:
8条回答
  • 2020-12-09 02:18

    You can not use maxlength and minlength function with input type=”number”

    Instead one can use regexp to solve this problem
    (?=.*[0-9]) - must contain only digit

    {10,10} - min length and max length must be 10. You can change these values

    <input type="number" pattern="(?=.*[0-9]).{10,10}" />
    

    Use

    pattern="(?=.*[0-9]).{10,10}"
    
    0 讨论(0)
  • 2020-12-09 02:21

    You may try to user type="text" and oninput as below. This will let the numbers only.

    <input type="text" maxlength="3" oninput="this.value=this.value.replace(/[^0-9]/g,'');" id="myId"/>
    
    0 讨论(0)
提交回复
热议问题