ValidatorOptional

cardNumber

Check a card number: 13 to 19 digits with a valid Luhn checksum.

Installation

Import the class, and @use the entry point in your global styles: without it the component renders unstyled.

import { Component } from '@angular/core';
import { WrInput } from 'ngwr/input';
import { WrValidators } from 'ngwr/validators';

@Component({ imports: [WrInput] })
export class MyComponent {}

Live demo

Try the test number 4242 4242 4242 4242.

errors: null

Usage

import { FormControl } from '@angular/forms';
import { WrValidators } from 'ngwr/validators';

const card = new FormControl('', [WrValidators.cardNumber]);

card.setValue('4242 4242 4242 4242'); // valid
card.setValue('1234567890123456');    // { cardNumber: true }

API

NameDescriptionTypeDefault
signatureAdd it to a validators array.ValidatorFn—
accepted13 to 19 digits, valid Luhn checksum. Spaces and dashes are ignored.string—
error keyOn failure: { cardNumber: true }. Empty value passes.{ cardNumber: true }—

See also