Last active
July 18, 2019 20:27
-
-
Save jaime-olivares/c462668c0b657a503a01723f5d4a8251 to your computer and use it in GitHub Desktop.
Verify National Provider Identifier
This file contains 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 verifyNPI(npi) | |
{ | |
if (!(/^[0-9]{10}$/.test(npi))) | |
return null; | |
var count = 0; | |
for (i = 8; i >= 0; i--) | |
{ | |
if (i % 2 == 0) | |
{ | |
var tempNum = npi.charAt(i) * 2; | |
if (tempNum >= 10) | |
count += (tempNum % 10) + 1; | |
else | |
count += tempNum; | |
} | |
else | |
{ | |
count += npi.charAt(i) * 1; | |
} | |
} | |
var checkDigit = (1006 - count) % 10; | |
return npi.charAt(9) == checkDigit; | |
} | |
/* | |
usage: | |
var result = verifyNPI("123457893"); | |
document.body.innerText = result || "false"; | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment