I have a web application where I'm specifying an input field to be a number using the HTML5 property type="number".
<input type="number" value="123456" />
By specifying the type, Chrome automatically formats the value to include a comma (123,456). In other browsers it does not format the number, but it also does not prevent non-numeric characters.
In this case, I don't want the comma to be added. Is there any way to turn off localized formatting?
This is occurring because of the behavior associated with the HTML5
number
input type in Chromium, and you are definitely not the only one that doesn't care for this.I have worked around this issue in the past by using the
text
type. For example, this has worked well (tested just now in Chrome 11.0.696.71):This behavior of the
number
type (to me, at least) is definitely a bug, because the HTML5 standard specifies thenumber
should have the following value when formatted for display:And the standard defines a "valid floating point" number here, and as far as I can see, including grouping characters is not expected.
Update
I've isolated the issue to the following code down in the guts of WebKit. I've included the line that fixes the issue here as well:
I'm on vacation next week, but plan on submitting this patch to the WebKit team when I return. Once they (hopefully) accept the patch, Chromium should pull it in as part of its normal refresh process.
There are a couple of extra properties that you can check including
valueAsNumber
.Chrome attempts to provide you with the best input method possible based on the input type. In this case, number also has some extra abilities such as toggle up and down.
The point of this, is if the number isn't valid, you will be able to detect that there are errors and also set the styling
input[type=number]:invalid { background-color: red; }