HTML Number Input Generator
WM-58Set the label, range and step on a few lines and get a number input whose min, max and starting value are checked before the HTML is written.
Runs in your browser. Nothing is uploaded.
Attribute lines: label:, name:, id:, value:, min:, max:, step:, placeholder:, hint:
HTML
How to use it
- Write label: and name:, then optional value:, min:, max:, step:, placeholder: and hint: lines.
- Tick Required if the field must be filled in.
- Pick label style, wrapper and an optional form.
- Copy the HTML into your page.
How step, min and max work together
The step attribute sets the allowed increments and also what the spinner buttons and arrow keys change. The allowed values count from min, so min 1 with step 2 accepts 1, 3 and 5 but not 2. With no step the default is 1, which means whole numbers; to allow decimals write a step such as 0.01, or the word any to accept every value. The tool checks that min is not above max, that step is above zero and that the starting value is inside the range.
When not to use a number input
A number input is for quantities that you add, subtract or compare. It is a poor fit for postcodes, phone numbers, card numbers and other identifiers: they can start with zero, contain spaces, and should never be changed by a scroll wheel or spinner. Use a text input for those, with a pattern or a numeric keyboard hint. The standard also treats scientific notation like 1e3 as a valid number, so a number field may accept the letter e.
What the browser does and the tool does not
A browser does not change an out-of-range value you type; it reports the problem when the form is submitted. Decimal steps can show tiny floating-point differences, as in any language. The value read from script is a string, so convert it before doing sums. A hint line is linked with aria-describedby, and the label with for and id. The tool does not store anything.
Error messages and what to do
| Message | What it means | Fix |
|---|---|---|
step must be a number above 0, or the word any. | The step line is zero or negative. | Use a value such as 1, 0.5 or any. |
min cannot be larger than max. | The min line is greater than max. | Swap the two values. |
Questions people ask
How do I limit a number input to a range?
Add min and max. The browser then flags values outside the range when the form is submitted. Check the range again on the server. The spinner buttons and arrow keys also stop at the limits. Browsers show a message at submit time, and the wording differs between them.
How do I allow decimals in an input type=number?
Set step to the smallest increment, such as 0.01, or to any. The default step of 1 allows whole numbers only. For money, 0.01 is a common step. Remember that values like 0.1 plus 0.2 can show small floating-point differences in script.
Why does my number input accept the letter e?
The standard counts floating-point numbers like 1e3 as valid, so the field lets you type e. If you only want digits, use a text input with a pattern.
Should I use type=number for a phone number?
No. Phone numbers, postcodes and card numbers are identifiers, not quantities. Use a text input with an inputmode hint instead. Those values can begin with zero, include plus signs or spaces, and should not change when someone scrolls over the field.
What type does the value have in JavaScript?
A string. Use Number or parseFloat before arithmetic. An empty or invalid entry gives an empty string. An empty string means the field is empty or holds text the browser could not read as a number. Check for that before you convert.
About this tool
Builds a label and an input of type number. Values for min, max and step are checked before the HTML is written: min cannot exceed max, step must be above zero (or the word any), and a starting value must sit inside the range. Valid steps count from min, so min 1 with step 2 allows 1, 3, 5. Decimal steps such as 0.1 can show floating-point oddities in the browser. A number input is for quantities, not for postcodes, phone numbers or card numbers, which are better as text inputs. A browser does not change an out-of-range value you type; it reports the problem when the form is submitted.