angular 2 pipe for credit cards

Viewed 4336

I want to create a pipe, that adds a space every 4 digits of a credit/debit card number.

this one hides all but the last 4 digits.

export class CreditCardMaskPipe implements PipeTransform {
  transform(plainCreditCard: string): string {
    const visibleDigits = 4;
    let maskedSection = plainCreditCard.slice(0, -visibleDigits);
    let visibleSection = plainCreditCard.slice(-visibleDigits);
    return maskedSection.replace(/./g, '*') + visibleSection;
  }
}

something like this but to add the spaces.

4 Answers

My answer is to late but it can be helpful for other who search for similar problems NGX Mask is really helpful in transforming card, phone numbers etc...

https://www.npmjs.com/package/ngx-mask

<span>{{creditCard | mask: '0000 0000 0000 0000'}}</span>
Related