Skip to content

Instantly share code, notes, and snippets.

@gnovaro
Last active May 27, 2022 11:31
Show Gist options
  • Save gnovaro/b56d015b53abcb33b0652d1de169eb67 to your computer and use it in GitHub Desktop.
Save gnovaro/b56d015b53abcb33b0652d1de169eb67 to your computer and use it in GitHub Desktop.
Format credit card
function formatCrediCard(cardtype, number)
{
let format;
if(cardtype == 'amex')
{
//XXXX XXXXXX XXXXX
format = number.substr(0,4)+" "+number.substr(4,6)+" "+number.substr(10,5);
} else {
//XXXX XXXX XXXX XXXX
format = number.substr(0,4)+" "+number.substr(4,4)+" "+number.substr(8,4)+" "+number.substr(12,4);
}
return format;
}
//Fake credit card generation https://www.fakenamegenerator.com/
//Example
console.log(formatCard('amex','347104348907272'));
console.log(formatCard('mastercard','5102186963653038'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment