Payment card
Payment card numbers (credit or debit), Luhn check only.
Validate
- JavaScript library
- Python library
- Go library2 cases fail
- Ruby library3 cases fail
- Rust library2 cases fail
- .NET library2 cases fail
- Erlang library
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 - 1has 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.
| Parameter | Type | Required |
|---|---|---|
value | string | number | yes |
| returns | boolean |
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/javascriptTry it with JavaScript isValidCreditCard
Shared test cases (42) and the result in each library creditCard.isValid
Official sources
Last updated on
