Here's a pure CSS solution
HTML:
<input value="1234567890" class="my-input" />
CSS:
input {
font-family: monospace;
font-size: 1em;
width: 3.2em;
padding: 0;
}
using width
expressed in em
unit BUT:
- you've to use a monospace font (or an m would be larger than an i)
1em
is equal to the height of an M in the particular monospace font the user will finally see in its system, not its width. With my current Windows Vista, I set a width of 3.2em with default monospace but your mileage will likely vary.
Thus you've to be particular meticulous in your chosen set of fonts and test on various Windows OS (XP, Vista, Win7 and Win8 all have their particular fonts installed, also depending on MS Office and Adobe Reader installed or not and, if web designers/developers are targeted, tools like Visual thing and Creative blah), OS X and flavors of Linux. And mobiles. Or you could begin your font-family
by a font downloaded via @font-face
and then continue with fonts having similar widths on each OS...
If CSS are disabled or customized by the user, that's a failure so you shouldn't rely on the width of the input for conveying meaning or doing a certain trick. That's not robust enough (and blind users will obviously neither care nor perceive that your input is visually limited to the display of 5 characters but still contains more characters).