How can I limit possible inputs in a HTML5 “number” element?

前端 未结 26 2534
感动是毒
感动是毒 2020-11-22 05:12

For element, maxlength is not working. How can I restrict the maxlength for that number element?

相关标签:
26条回答
  • 2020-11-22 05:39

    And you can add a max attribute that will specify the highest possible number that you may insert

    <input type="number" max="999" />
    

    if you add both a max and a min value you can specify the range of allowed values:

    <input type="number" min="1" max="999" />
    

    The above will still not stop a user from manually entering a value outside of the specified range. Instead he will be displayed a popup telling him to enter a value within this range upon submitting the form as shown in this screenshot:

    0 讨论(0)
  • 2020-11-22 05:39

    a simple way to set maxlength for number inputs is:

    <input type="number" onkeypress="return this.value.length < 4;" oninput="if(this.value.length>=4) { this.value = this.value.slice(0,4); }" />
    
    0 讨论(0)
提交回复
热议问题