Add Maxlength To Inputs With Number Type Using Only HTML

Posted By Weston Ganger

I wanted a simple way to restrict the number of allowing digits in my number input. You must change your input's type to 'text' and add a pattern

<pre>
<code class="language-markup">
<input type="text" pattern="\d*" maxlength="2">


This is much simpler than using a JS library and is supported IE 10+ and on mobile as well.


Related External Links:

- [Stack Overflow - how-to-add-maxlength-for-html5-input-type-number-element](http://stackoverflow.com/questions/8354975/how-to-add-maxlength-for-html5-input-type-number-element)

Article Topic:Software Development - HTML / CSS

Date:February 09, 2017