2

I am using <input type=number> where I place values using Javascript. I'd like to format these numbers to have always one decimal. Chrome stubbornly strips out of trailing zero from the numbers if they have any e.g. 1.0 -> 1.

Apparently I should set pattern attribute of the control. However I am not sure what kind of values Chrome accepts here and what would be the correct pattern for formatting numbers.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
  • Take a look at this question: [How can I make the HTML5 number field display trailing zeroes?](http://stackoverflow.com/questions/7790561/how-can-i-make-the-html5-number-field-display-trailing-zeroes) – Christian Schmidt Oct 23 '13 at 11:12

1 Answers1

1

pattern is used to specify a regular expression that any value the user supplies should match. Something like pattern='[0-9]+\.[0-9]' should specify 1 or more digits, a decimal, then 1 digit. You might also want to set the step size to 0.1 (step=0.1) to force only 1 decimal. I don't know if chrome will respect the pattern and size attributes or not, but that is how to specify them.

Perkins
  • 2,409
  • 25
  • 23
  • 1
    Pattern only flags inputs that don't conform, it doesn't prevent the bad value from being entered, if chrome is auto-stripping the trailing 0, it might do that and then flag the field as invalid, so I don't know that it will actually help you to have pattern specified. – Perkins May 14 '13 at 20:39