Payment card

Payment card numbers (credit or debit), Luhn check only.

  • Parity matrix

Validate

Validates a payment card number (credit or debit): 12 to 19 digits and a Luhn check digit.

  • The function does not detect the brand, look up the issuer range, or check the expiry or the CVV.
  • The function rejects a number whose digits are all the same, even though it passes Luhn.
  • Accepts a string or a number. The mask characters (whitespace, ., - and /, the set the CPF and CNPJ accept) go between any two digits, a run of them included (4111 - 1111 - 1111 - 1111), because the printed grouping changes with the brand (4-4-4-4, 4-6-5, 4-6-4). Whitespace around the value is ignored.
  • Any other character makes the number invalid instead of being dropped: letters (4111a1111b1111c1111), ,, _, parentheses, or a mask character that is not between two digits (a leading -, a trailing .).
  • A number is accepted only when it is a non-negative safe integer. A number above 2^53 - 1 has already been rounded when it arrives, and a negative or fractional one is not a card number, so pass a longer number as a string.
ParameterTypeRequired
valuestring | numberyes
returnsboolean

Check if a payment card number (credit or debit) is valid using the Luhn algorithm. Only the digit count (12 to 19) and the Luhn check digit are checked. There is no brand detection (Visa, Mastercard, Amex...), issuer range lookup or expiration/CVV checks.

  • Accepts a string or a number, with the mask characters (whitespace, ., - and /) anywhere between the digits.
import { isValidCreditCard } from '@brazilian-utils/brazilian-utils';

isValidCreditCard('4111111111111111'); // true (Visa test number)
isValidCreditCard('5555555555554444'); // true (Mastercard test number)
isValidCreditCard('378282246310005'); // true (American Express test number)
isValidCreditCard('4111 1111 1111 1111'); // true (spaced mask)
isValidCreditCard('4111 - 1111 - 1111 - 1111'); // true (a run of separators between the digits)
isValidCreditCard('4111.1111/1111-1111'); // true (any of the mask characters)
isValidCreditCard('4111111111111112'); // false (bad check digit)
isValidCreditCard('0000000000000000'); // false (every digit the same, though the Luhn check passes)
isValidCreditCard('4111a1111b1111c1111'); // false (letters between the digits)
isValidCreditCard(4111111111111111111); // false (above 2^53 - 1, pass it as a string)

Source: ISO/IEC 7812-1.

Code: brazilian-utils/javascript
Try it with JavaScript isValidCreditCard
The inputs start with the first shared case. Change one to see the new result.

Runs @brazilian-utils/brazilian-utils 2.5.0 in your browser.

Shared test cases (42) and the result in each library creditCard.isValid

Official sources

Last updated on

On this page