How to restrict user input character length of HTML5 input type=“number”?

前端 未结 4 421
自闭症患者
自闭症患者 2021-01-13 11:44

How do I restrict user input to a given length in an HTML5 input[type=number] textbox?

the answers to

How can I limit possible inputs in a HTML

4条回答
  •  天命终不由人
    2021-01-13 12:16

    I don't know my answer is useful for you? But i happy if i can help you. you should write a js method and then use it in your html page.

    function limit(element) {
      var max_chars = 2;
    
      if(element.value.length > max_chars) {
        element.value = element.value.substr(0, max_chars);
      }
    }
    
    
    
    

提交回复
热议问题