Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any algorithm which check right NPI (National Provider Identification) number?

Basically I'm developing application using asp.net and I want to write Custom validator which will check right NPI number.

like image 624
Sagar Avatar asked Oct 19 '25 07:10

Sagar


2 Answers

Wikipedia says:

A 10-digit NPI Number may be validated using the Luhn algorithm by prefixing "80840" to the 10-digit number.

Obviously that just validates the check digit. To find whether a number is actually assigned, I think you need a list. There's an online lookup, for example. Even without that, there may be additional checks you can do if you know anything about how HIPAA hands the numbers out.

like image 174
Steve Jessop Avatar answered Oct 21 '25 21:10

Steve Jessop


In case someone has found this post but is looking for this in Java, here is a java program that does this:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class NpiUtil {
    private static final String NPI_PATTERN_STRING = "[0-9]{10}";
    private String npi;

    public static boolean isValidNpi(final String npi) {
        return new NpiUtil(npi).isValid();
    }

    private static String fromCharCode(int... codePoints) {
        return new String(codePoints, 0, codePoints.length);
    }

    public static String generateRandomNpi() {
        Double randomNumber = Math.floor(Math.random() * (999999999 - 100000000 + 1) + 100000000);
        int tmp;
        int sum;
        int i;
        int j;
        String npi9 = String.valueOf(randomNumber.intValue());
        i = npi9.length();
        if ((i == 14) && (npi9.indexOf("80840", 5) == 0))
            sum = 0;
        else if (i == 9)
            sum = 24;
        else {
            return "!";
        }

        j = 1;
        while (i != 0) {
            tmp = npi9.charAt(i - 1) - new String("0").charAt(0);
            if ((j++ % 2) != 0) {
                if ((tmp <<= 1) > 9) {
                    tmp -= 10;
                    tmp++;
                }
            }
            sum += tmp;
            i--;
        }
        String checkDigit = fromCharCode(((10 - (sum % 10)) % 10 + 48));
        return randomNumber.longValue() + checkDigit;
    }

    private NpiUtil(final String npi) {
        this.npi = npi.trim();
    }

    private boolean isValid() {
        return this.npi.length() == 10 && this.complies();
    }

    private boolean complies() {
        if (!this.npi.matches(NPI_PATTERN_STRING)) {
            return false;
        }
        final Character lastDigit = this.npi.charAt(9);
        final List<Integer> everyOther = this.listWithEveryOtherDoubled(this.npi.substring(0, 9));
        int sum = 0;
        for (final Integer num : everyOther) {
            sum += sumOfDigits(num);
        }
        final int total = sum + 24;
        final int units = total % 10;
        final int checkDigit = (units != 0) ? (10 - units) : units;
        return Character.getNumericValue(lastDigit) == checkDigit;
    }

    private List<Integer> listWithEveryOtherDoubled(final String str) {
        final List<Integer> nums = new ArrayList<Integer>();
        for (int i = 0; i < str.length(); ++i) {
            if (i % 2 == 0) {
                nums.add(2 * Character.getNumericValue(str.charAt(i)));
            } else {
                nums.add(Character.getNumericValue(str.charAt(i)));
            }
        }
        return nums;
    }

    private static int sumOfDigits(final int number) {
        int num = number;
        int sum = 0;
        while (num > 0) {
            sum += num % 10;
            num /= 10;
        }
        return sum;
    }

    public static void main(final String[] args) {
        System.out.println("Hello, World!");
        System.out.println("1234567890".matches(NPI_PATTERN_STRING));
        System.out.println("123456789".matches(NPI_PATTERN_STRING));
    }
}

Usage:

import org.junit.Assert;
import org.junit.Test;

public class NpiUtilTest {
    @Test
    public void testNpiUtil() {
        for (int i = 0; i < 100; ++i) {
            Assert.assertTrue(NpiUtil.isValidNpi(NpiUtil.generateRandomNpi())); 
        }
    }
}
like image 32
Nicholas DiPiazza Avatar answered Oct 21 '25 21:10

Nicholas DiPiazza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!