As other comments have stated, type="number" inputs do not have a maxlength attribute and, instead, have a min and max attribute. maxlength ignored for input type="number" in Chrome, From MDN's documentation for . Others using evt.keycode seem to fail on my android, One very minor mod, change "this.id" to "this", numberOnly(id) to numberOnly(element), then you don't need the first line of numberOnly and it works without an id, in some browsers (specifically mobile), the. This is nice one . If the value of the type attribute is text, email, search, password, tel, or url, this attribute specifies the maximum number of characters (in Unicode code points) that the user can enter; for other control types, it is ignored. This isn't even the "falsehoods about phone numbers" piece I was looking for, but it has a lot of good points. the element. Numbers only and maxlength work perfect. So with 4 digits, max should be 9999, 5 digits 99999 and so on. You must change your input’s type to ‘text’ and add a pattern. The maximum number of characters that will be accepted as input. Disabling Chrome cache for website development. Stack Overflow for Teams is a private, secure spot for you and