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

前端 未结 26 2831
感动是毒
感动是毒 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:23

    Maycow Moura's answer was a good start. However, his solution means that when you enter the second digit all editing of the field stops. So you cannot change values or delete any characters.

    The following code stops at 2, but allows editing to continue;

    //MaxLength 2
    onKeyDown="if(this.value.length==2) this.value = this.value.slice(0, - 1);"
    

提交回复
热议问题