The HTML <input>
element with type "number"
is used to create an input field where users can enter numeric values.
This type of input provides built-in validation for numeric input, allowing only numbers within specified constraints.
HTML <input type="number">
Attributes
type
: Defines the type of the input field. For numeric input, it should be set to "number"
.
<input type="number">
name
: Specifies the name of the input field. This is used when submitting form data to identify the input field.
<input type="number" name="age">
value
: Sets a default value for the input field. This is the value that appears in the field when the page loads.
<input type="number" value="25">
placeholder
: Provides a short hint or example of the expected value. This text is displayed in the input field when it is empty and disappears when the user starts typing.
<input type="number" placeholder="Enter your age">
min
: Specifies the minimum value allowed in the input field. If the user tries to enter a value below this, the input will be rejected.
<input type="number" min="0" placeholder="Enter your age">
max
: Specifies the maximum value allowed in the input field. If the user tries to enter a value above this, the input will be rejected.
<input type="number" max="120" placeholder="Enter your age">
step
: Defines the legal number intervals. For example, a step value of 5
means the input can be 0, 5, 10, etc.
<input type="number" step="5" placeholder="Enter a multiple of 5">
required
: Makes the input field mandatory. The form cannot be submitted until a value is entered in this field.
<input type="number" required placeholder="Enter your age">