Created
November 8, 2018 12:20
-
-
Save cbrannen9a/b92dbb0e26ba86dac3284be21296e5e0 to your computer and use it in GitHub Desktop.
Partial demo of credit card check using Regex and luhnCheck methods
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
const validateCardNumber = (number) => { | |
var regex = new RegExp("^[0-9]{16}$"); | |
if (!regex.test(number)) { return false; } | |
return luhnCheck(number); | |
} | |
const luhnCheck = (val) => { | |
var sum = 0; | |
for (var i = 0; i < val.length; i++) { | |
var intVal = parseInt(val.substr(i, 1)); | |
if (i % 2 === 0) { | |
intVal *= 2; | |
if (intVal > 9) { | |
intVal = 1 + (intVal % 10); | |
} | |
} | |
sum += intVal; | |
} | |
return (sum % 10) === 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment