htmlformsgoogle-chrome

What is the correct input type for credit card numbers?


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!


Solution

  • HTML

    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:

    <form>
      <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" required>
    
      <button>Submit</button>
    </form>

    JavaScript

    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.