4 elements of type
Show 5 are used to let the user enter a number. They include built-in validation to reject non-numerical entries. The browser may opt to provide stepper arrows to let the user increase and decrease the value using their mouse or by tapping with a fingertip. On browsers that don't support inputs of type
5, a
5 input falls back to type
8. A number representing the value of the number entered into the input. You can set a default value for the input by including a number inside the attribute, like so:
In addition to the attributes commonly supported by all
4 types, inputs of type
5 support these attributes. The values of the list attribute is the
3 of a
4 element located in the same document. The
4 provides a list of predefined values to suggest to the user for this input. Any values in the list that are not compatible with the are not included in the suggested options. The values provided are suggestions, not requirements: users can select from this predefined list or provide a different value. The maximum value to accept for this input. If the entered into the element exceeds this, the element fails constraint validation. If the value of the
7 attribute isn't a number, then the element has no maximum value. This value must be greater than or equal to the value of the
0 attribute. The minimum value to accept for this input. If the of the element is less than this, the element fails constraint validation. If a value is specified for
0 that isn't a valid number, the input has no minimum value. This value must be less than or equal to the value of the
7 attribute. The
5 attribute is a string that provides a brief hint to the user as to what kind of information is expected in the field. It should be a word or short phrase that demonstrates the expected type of data, rather than an explanatory message. The text must not include carriage returns or line feeds. If the control's content has one directionality (LTR or RTL) but needs to present the placeholder in the opposite directionality, you can use Unicode bidirectional algorithm formatting characters to override directionality within the placeholder; see How to use Unicode controls for bidi text for more information. Note: Avoid using the
5 attribute if you can. It is not as semantically useful as other ways to explain your form, and can cause unexpected technical issues with your content. See for more information. A Boolean attribute which, if present, means this field cannot be edited by the user. Its
9 can, however, still be changed by JavaScript code directly setting the
1
9 property. Note: Because a read-only field cannot have a value,
3 does not have any effect on inputs with the
9 attribute also specified. The
5 attribute is a number that specifies the granularity that the value must adhere to, or the special value
7, which is described below. Only values which are equal to the basis for stepping ( if specified, otherwise, and an appropriate default value if neither of those is provided) are valid. A string value of
7 means that no stepping is implied, and any value is allowed (barring other constraints, such as and ). Note: When the data entered by the user doesn't adhere to the stepping configuration, the user agent may round to the nearest valid value, preferring numbers in the positive direction when there are two equally close options. The default stepping value for
5 inputs is
4, allowing only integers to be entered—unless the stepping base is not an integer. The
5 input type should only be used for incremental numbers, especially when spinbutton incrementing and decrementing are helpful to user experience. The
5 input type is not appropriate for values that happen to only consist of numbers but aren't strictly speaking a number, such as postal codes in many countries or credit card numbers. For non-numeric inputs, consider using a different input type, such as
7 or other
4 type with the attribute:
0 elements can help simplify your work when building the user interface and logic for entering numbers into a form. When you create a number input with the proper
6 value,
5, you get automatic validation that the entered text is a number, and usually a set of up and down buttons to step the value up and down. Warning: Logically, you should not be able to enter characters inside a number input other than numbers. Some browsers allow invalid characters, others do not; see Firefox bug 1398528. Note: A user can tinker with your HTML behind the scenes, so your site must not use simple client-side validation for any security purposes. You must verify on the server side any transaction in which the provided value may have security implications of any kind. Mobile browsers further help with the user experience by showing a special keyboard more suited for entering numbers when the user tries to enter a value. In its most basic form, a number input can be implemented like this:
A number input is considered valid when empty and when a single number is entered, but is otherwise invalid. If the attribute is used, the input is no longer considered valid when empty. Sometimes it's helpful to offer an in-context hint as to what form the input data should take. This can be especially important if the page design doesn't offer descriptive labels for each
4. This is where placeholders come in. A placeholder is a value most commonly used to provide a hint as to the format the input should take
9. It is displayed inside the edit box when the element's
9 is
7. Once data is entered into the box, the placeholder disappears; if the box is emptied, the placeholder reappears. Here, we have an
5 input with the placeholder "
9". Note how the placeholder disappears and reappears as you manipulate the contents of the edit field.
By default, the up and down buttons provided for you to step the number up and down will step the value up and down by 1. You can change this by providing a attribute, which takes as its value a number specifying the step amount. Our above example contains a placeholder saying that the value should be a multiple of 10, so it makes sense to add a
5 value of
2:
In this example, you should find that the up and down step arrows will increase and decrease the value by 10 each time, not 1. You can still manually enter a number that's not a multiple of 10, but it will be considered invalid. You can use the and attributes to specify a minimum and maximum value that the field can have. For example, let's give our example a minimum of
5, and a maximum of
6:
In this updated version, you should find that the up and down step buttons will not allow you to go below 0 or above 100. You can still manually enter a number outside these bounds, but it will be considered invalid. One issue with number inputs is that their step size is 1 by default. If you try to enter a number with a decimal (such as "1.0"), it will be considered invalid. If you want to enter a value that requires decimals, you'll need to reflect this in the
5 value (e.g.
8 to allow decimals to two decimal places). Here's a simple example:
See that this example allows any value between
9 and
0, with decimals to two places. For example, "9.52" is valid, but "9.521" is not.
4 elements of type
5 don't support form sizing attributes such as . You'll have to resort to CSS to change the size of these controls. For example, to adjust the width of the input to be only as wide as is needed to enter a three-digit number, we can change our HTML to include an and to shorten our placeholder since the field will be too narrow for the text we have been using so far:
Then we add some CSS to narrow the width of the element with the
3 selector
6: The result looks like this: You can provide a list of default options from which the user can select by specifying the attribute, which contains as its value the of a
4, which in turn contains one
0 element per suggested value. Each
1's
9 is the corresponding suggested value for the number entry box.
We have already mentioned a number of validation features of
5 inputs, but let's review them now:
The following example exhibits all of the above features, as well as using some CSS to display valid and invalid icons, depending on the
00's value:
Try submitting the form with different invalid values entered — e.g., no value; a value below 0 or above 100; a value that is not a multiple of 10; or a non-numerical value — and see how the error messages the browser gives you differ with different ones. The CSS applied to this example is as follows:
0 Here we use the
01 and
02 pseudo classes to display an appropriate invalid or valid icon as generated content on the adjacent
03 element, as a visual indicator of validity. We put it on a separate
03 element for added flexibility. Some browsers don't display generated content very effectively on some types of form inputs. (Read, for example, the section on .) Warning: HTML form validation is not a substitute for server-side scripts that ensure that the entered data is in the proper format! It's far too easy for someone to make adjustments to the HTML that allow them to bypass the validation, or to remove it entirely. It's also possible for someone to bypass your HTML and submit the data directly to your server. If your server-side code fails to validate the data it receives, disaster could strike when improperly-formatted data is submitted (or data which is too large, is of the wrong type, and so forth).
0 elements do not support use of the attribute for making entered values conform to a specific regex pattern. The rationale for this is that number inputs won't be valid if they contain anything except numbers, and you can constrain the minimum and maximum number of valid digits using the and attributes (as explained above). We've already covered the fact that by default, the increment is
4, and you can use the attribute to allow decimal inputs. Let's take a closer look. In the following example is a form for entering the user's height. It defaults to accepting a height in meters, but you can click the relevant button to change the form to accept feet and inches instead. The input for the height in meters accepts decimals to two places. The HTML looks like this:
1 You'll see that we are using many of the attributes we've already looked at in the article earlier on. Since we want to accept a meter value in centimeters, we've set the
5 value to
13, so that values like 1.78 are not seen as invalid. We've also provided a placeholder for that input. We've hidden the feet and inches inputs initially using
14, so that meters is the default entry type. Now, onto the CSS. This looks very similar to the validation styling we saw before; nothing remarkable here.
2 And finally, the JavaScript:
3 After declaring a few variables, an event listener is added to the
15 to control the switching mechanism. This is pretty simple, mostly involving changing over the button's
16 and
17, and updating the display values of the two sets of inputs when the button is pressed. (Note that we're not converting back and forth between meters and feet/inches here, which a real-life web application would probably do.) Note: When the user clicks the button, the
3 attribute(s) are removed from the input(s) we are hiding, and empty the
9 attribute(s). This is so the form can be submitted if both input sets aren't filled in. It also ensures that the form won't submit data that the user didn't mean to. If you didn't do this, you'd have to fill in both feet/inches and meters to submit the form! The implicit role for the
0 element is
21. If spinbutton is not an important feature for your form control, consider not using
22. Instead, use
23 along with a
07 attribute that limits the characters to numbers and associated characters. With
0, there is a risk of users accidentally incrementing a number when they're trying to do something else. Additionally, if users try to enter something that's not a number, there's no explicit feedback about what they're doing wrong. Also consider using the
26 attribute to help users complete forms more quickly and with fewer chances of errors. For example, to enable autofill on a zip code field, set
27. |