Skip to content

luhn ​

A bank card number with a valid check digit (Luhn algorithm), 12–19 digits.

Signature ​

ts
function luhn(msg?: Message): Action<string>
function isLuhn(value: string): boolean

Parameters ​

ParameterTypeDescription
msgMessageCustom error message

Error codes ​

CodeWhenDefault message
luhnInvalid checksum or formatInvalid card number

You can change the texts: see Messages & i18n.

Notes ​

Catches typos and random digits. Doesn't check expiry, whether the card exists or the brand — tinyval-mask has cardBrand for that.

  • Spaces and dashes are allowed: a masked value can be validated.
  • The isLuhn function is available separately — for checks without a schema.
  • Format the input with a mask from tinyval-mask.

Example ​

ts
const Payment = object({ card: pipe(string(), luhn()) });

isLuhn('4111 1111 1111 1111'); // true

See also ​