Skip to content

Instantly share code, notes, and snippets.

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