-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow pattern attribute on input type="number" #1086
Comments
I don't believe this has anything to do with the pattern attribute. Browsers use regular expressions, sure, but they don't use the pattern attribute.
It sounds like what you're asking for here is not really related to custom patterns, but related to browser UI. You wish browsers allowed different ways of entering things in number inputs than just allowing numbers. This is already allowed by the spec, however. The spec makes no judgments on browser UI: a number field could be a textbox with spinners, something with a slider, or something where you type words like "one thousand and forty three", or something where you type things like "$0.50" or "123 USD". The spec doesn't control that. It sounds like you're interested in getting browsers to update their UI, which is probably best done via browser issue trackers. |
I see your point about what the spec doesn't say, but I think the main purpose of my suggestion is the current behavior most browsers do of only allowing numerical input is also itself a valid and common use case so some developers would prefer to turn off these new features. There's also backwards-compatibility: if all number inputs across the web suddenly start accepting $ and % characters for example, then suddenly you can type unexpected values and all sorts of form validation/JS code/server-posted values could break. I think this alone is motivation enough for browser makers to say they won't change it, hence the need for some other separate feature to allow this. Maybe instead there could be a new more relaxed input type, e.g. input type="numerical", specifically intended for use cases like currency etc.? From a web search/looking at StackOverflow it looks like a common problem. |
It sounds like you want |
@annevk - oh, I didn't know about that attribute - could browsers also use it to show a spinner? |
If they wanted to, I suppose. |
There's a difference between what's displayed in the UI, and what's reflected to JavaScript and to the server upon form submission. From https://html.spec.whatwg.org/multipage/forms.html#concept-fe-value:
|
I think we can close this, because there's nothing for the spec to do here; it already allows the desired flexibility, and it's simply up to browsers to decide if they want to work on their |
The
pattern
attribute has no effect on number inputs. However some browsers appear to use a pattern anyway to enforce number input only, judging by the error Chrome reports when trying to assign something else:The value must match to the following regular expression: -?(\d+|\d+\.\d+|\.\d+)([eE][-+]?\d+)?
It would be useful to allow a custom pattern for number inputs for several use cases:
€100
,0.50$
,£5
etc25%
,45°
etc10 000
,10,000
etc1+1
,1920/2
etcCurrently all of these use cases require using input type="text", which has two drawbacks:
This does raise some issues:
$100
?value
attribute is a string anyway, so this can simply return the full string. It's up to Javascript to parse it correctly, which developers are opting-in to doing by specifying a custom pattern.valueAsNumber
could also return a number parsed by simply filtering all valid numerical characters and callingparseFloat()
on the result. This works for all the use cases above except calculations, where the number will be calculated by Javascript separately anyway.$100
in to101
rather than$101
, but the browser probably can't know how to do that automatically. Again this can be solved in Javascript by listening for change events and re-applying the correct formatting.Not specifying a pattern would work identically to how it does at the moment, so this would be as backwards-compatible as possible.
The text was updated successfully, but these errors were encountered: