`` does not display a number keypad on iOS
To bring up a numeric keypad on iOS using <input type="number">
, include the pattern
attribute set to \\d*
. This specific regular expression instructs iOS to display the ideal numeric keypad.
Snippet:
The key here is the pattern
attribute with the \\d*
regex to access the keypad you need on iOS devices.
Keys to guarantee the numeric keypad
In addition to the basic pattern
attribute set to value \\d*
, more options are available for a smoother UX on iOS devices. The inputmode="decimal"
attribute is suitable for inputs requiring decimal numbers, presenting a keypad inclusive of fractional numbers.
For intricate number formats, such as telephone numbers, input type="tel"
serves as a reliable alternative. This triggers a numeric keypad, which is widely supported:
However, be aware of iOS version compatibility with the inputmode
attribute to ensure the expected behavior. Some older iOS versions might not support inputmode
correctly, so testing across different iOS versions is recommended.
Use JavaScript for validation
While configuring your HTML input field, consider JavaScript validation to ensure correct data reception. By adding event listeners to prevent non-numeric inputs, you increase the stability of your form:
Remember, the client-side validation should be in sync with server-side validation to take care of any unexpected non-numeric inputs.
Focus on user experience
UX plays an essential role when setting up input fields. All "number" type fields may not require a numeric keypad instantly visible. Address or zip code fields, where users might input letters or other characters, should have the appropriate attributes for the optimal keyboard display:
For inputs on iPad, handle ambiguous keys with extra care. iPads have varied keyboard layouts so make sure your method for suppressing undesired keys works effectively across different models.
Overcoming exceptions and enhancing UX
Although HTML5 offers us the type="number"
directive, it does present some quirks. iOS might not consider comma-separated or space-separated numbers as numeric inputs:
In such scenarios, UX could suffer. Therefore, use attribute modifiers such as inputmode
and pattern
to let the input type guide you. To securely handle keypress events and suppress undesired characters, use jQuery:
Ensure the UX is lenient by testing on different iOS versions and devices, especially when entering crucial details like payment or personal information.
Was this article helpful?