Skip to content

Instantly share code, notes, and snippets.

@jaime-olivares
Last active July 18, 2019 20:27
Show Gist options
  • Save jaime-olivares/c462668c0b657a503a01723f5d4a8251 to your computer and use it in GitHub Desktop.
Save jaime-olivares/c462668c0b657a503a01723f5d4a8251 to your computer and use it in GitHub Desktop.
Verify National Provider Identifier
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