Jon Schneider
Jon Schneider

Reputation: 26973

Why doesn't the input type="number" control support the maxlength attribute?

Why doesn't the HTML 5 specification for the input type="number" control include support for the maxlength attribute?

Browsing Stack Overflow for questions matching search terms like input type number maxlength, it seems that there are quite a few web applications out there with a requirement to display a control that requires the user to input a number, but also constrains the input field to a particular maximum number of characters.

Judging from the Stack Overflow answers to these questions, it seems like the solution that most applications end up landing on is either using some manner of Javascript "hack" to limit the count of characters appearing in the control, or else to use a input type="text" control (with its longstanding support for the maxlength attribute) instead of the newer input type="number".

It seems like a cleaner solution for this common requirement would be to allow maxlength to work with input type="number" controls.

I understand that a particular max attribute value may not always correspond directly to a maxlength attribute value -- e.g. if the maximum number to be allowed in a control is something like 50 instead of 99 -- but apart from that, what is the reason that the HTML 5 specification does not include support for the maxlength attribute?

Upvotes: 0

Views: 1535

Answers (2)

SamHuckaby
SamHuckaby

Reputation: 1162

Per the answer found here: https://stackoverflow.com/a/18510925/1507210

You could alternately use this syntax with a text field to get what you're looking for without the chance of introducing letters via the pattern field, which uses regex matching:

<input type="text" pattern="\d*" maxlength="4">

Though as I stated in my comment, I can see no reason you would ever want to restrict the length of a number that max would fail to solve the problem, and more gracefully.

An example would be, in a situation where your number control goes into the negative. Using a length attribute would break negative numbers because a number with a maxlength of 1 would not permit '-1' as it is two characters. By only allowing min and max, it prevents programmers who aren't thinking from writing code that will break as soon as someone tries to go into the negative numbers.

Upvotes: 1

marxin
marxin

Reputation: 3922

Because usually numeric values are converted to a specific type, integer for example. Signed integer is a 4 byte number and we know, what is max value and min value for this type, not max_length.

I think that's the reason.

Upvotes: 0

Related Questions