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

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

    Simple solution which will work on,

    • Input scroll events

    • Copy paste via keyboard

    • Copy paste via mouse

    • Input type etc cases

      
      

    See there is condition on this.value > 5, just update 5 with your max limit.

    Explanation:

    • If our input number is more then our limit update input value this.value with proper number Math.abs(this.value)

    • Else just make it to your max limit which is again 5.

提交回复
热议问题