TLDR: Which input type can utilize the mobile numeric keyboards AND implement masking with spaces injected and a max limit for CVV and Credit Card inputs, such as: XXXX XXXX XXXX XXXX
When building forms, I have seen a varied degree of consistency surrounding the correct input type for credit cards. Here are my discovered pros and cons for each:
type=text
type=number
type=tel
These are all the types that came to mind. If anyone has any other recommendations, please let me know!
Credit card numbers are numeric data that follow the 14-digit or 16-digit patterns for credit cards.
Payment card numbers are composed of 8 to 19 digits, The leading six or eight digits are the issuer identification number (IIN) sometimes referred to as the "bank identification number (BIN)". The remaining numbers, except the last digit, are the individual account identification number.
Credit card numbers are not strictly numbers. They are strings, but the numbers which make up the long 16 digit number can be exploded in order to validate the number by using the checksum of the number.
The <input type="number"> defines a field for entering a number. Use the following attributes to specify restrictions: max - specifies the maximum value allowed.
If you're trying to do this strictly with HTML, I believe the following is going to get you about as close as is currently possible:
<label for="ccn">Credit Card Number:</label>
<input id="ccn" type="tel" inputmode="numeric" pattern="[0-9\s]{13,19}" autocomplete="cc-number" maxlength="19" placeholder="xxxx xxxx xxxx xxxx">
inputmode
sets the keyboard type (in this case, numeric)pattern
enables validation (in this case, numbers and spaces with a length of between 13 and 19) and it helps with keyboard type for browsers that don't yet support inputmode
autocomplete
tells browser what should be autocompleted (in this case, a credit card number)maxLength
prevents the user from entering more than the set number of characters (in this case, 19, to include numbers and spaces)placeholder
gives the user an example (formatting hint)For a more robust solution, JavaScript is going to be required. And rather than roll your own solution, it'd probably be best to use a battle-tested library. Cleave.js (as you mentioned) is a popular choice.
There's an attribute inputmode
that's designed for that, it's not implemented yet (actually deprecated in HTML 5.2), but there's work done on it (FF/Chrome).
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input
And see this discussion: https://github.com/whatwg/html/issues/3290
For now set the autocomplete attribute to the correct value: https://developers.google.com/web/updates/2015/06/checkout-faster-with-autofill
or implement a customized input with mask like you're using now.
I’ve seen using type="tel"
on many places, for the reasons you mention.
I would not recommend type="number"
as then you have to fiddle with in/decrement arrows etc. And from certain point of view it is not a “number” in terms of what we usualy do with numbers (maths), see this comment on CSS tricks forum.
Another trick how to force numeric keyboard is using pattern="[0-9]*"
. This should work on iOS only. To make it work on Android as well, you have to combine it with the type="tel"
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With