Created
June 12, 2019 12:24
-
-
Save imparvez/b0a5e8b6816850d0d62361e90cfb55b4 to your computer and use it in GitHub Desktop.
Masking Credit Card Number using JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function maskCC(number) { | |
// If given parameter doesn't fall under below category, then return | |
// 1. Should be numbers(Every element should be a number) | |
// 2. Should be of length 16 | |
if(!(typeof number === 'string' && number.length === 16 && number.split('').map(Number).every(n => !isNaN(n)) )) { | |
return; | |
} | |
return [...new Array(3).fill('****'), number.slice(-4)].join('-') | |
} | |
console.log(maskCC('1234567887654321')) // ****-****-****-4321 | |
console.log(maskCC('aaaaaaaaaaaaaaaa')) // undefined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment