Cards-format
jQuery.payment by TypeScripts service
How to install
npm i cards-format --save
How to import
For TypeScript usage there is a index.d.ts in node_modules folder
;
or
var CardsFormat = ;
API
restrictNumeric
(e: KeyboardEvent): boolean - General numeric input restriction.formatCardNumber
(e: KeyboardEvent, callback: (value: string) => void): void
Formats card numbers:
- Includes a space between every 4 digits
- Restricts input to numbers
- Limits to 16 numbers
- Supports American Express formatting
- Adds a class of the card type (e.g. 'visa') to the input
Example
CardsFormat.formatCardNumberevent,;
Then, when the payment form is submitted, you can validate the card number on the client-side:
validateCardNumber
(num: number): boolean; - Full validatevalidateCardNumberSimple
(num: number): boolean; - Simple validate
Example
;
cardFromNumber
(num: number): Card;
Where type Card is:
declare
formatBackCardNumber
(e, callback: (value: string) => void)reFormatCardNumber
(event, (value: string))replaceFullWidthChars
(str: string): stringcardType
(num: number): CardTypes - Returns a card type.reFormatExpiry
(e, callback: (value: string) => void): void
Example
CardsFormat.reFormatExpiryevent,;
cardExpiryVal
(value: string): { month: number; year: number; };
Parses a credit card expiry in the form of MM/YYYY, returning an object containing the month and year. Shorthand years, such as 13 are also supported (and converted into the longhand, e.g. 2013).
Example
CardsFormat.cardExpiryVal'03 / 2025'; //=> {month: 3, year: 2025} CardsFormat.cardExpiryVal'05 / 04'; //=> {month: 5, year: 2004} CardsFormat.cardExpiryVal'cardExpiryVal' //=> {month: 4, year: 2020}
This function doesn't perform any validation of the month or year; use $.payment.validateCardExpiry(month, year) for that.
validateCardExpiry
(month: number, year: number): boolean;
For reBuild
$ ./production