Last active
July 18, 2019 20:25
-
-
Save jaime-olivares/319c1488b152c0338004dd2378fc8537 to your computer and use it in GitHub Desktop.
Verify National Provider Identifier
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
/* | |
usage: | |
bool? result = verifyNPI("1234567893"); | |
Console.WriteLine(result ?? false); | |
*/ | |
public static bool? verifyNPI(string npi) | |
{ | |
if (!Regex.IsMatch(npi, @"^[0-9]{10}$")) | |
return null; | |
var count = 0; | |
for (int i = 8; i >= 0; i--) | |
{ | |
if (i % 2 == 0) | |
{ | |
var tempNum = int.Parse(npi[i].ToString()) * 2; | |
if (tempNum >= 10) | |
count += (tempNum % 10) + 1; | |
else | |
count += tempNum; | |
} | |
else | |
{ | |
count += int.Parse(npi[i].ToString()); | |
} | |
} | |
var checkDigit = (1006 - count) % 10; | |
return npi[9].ToString() == checkDigit.ToString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment