Recently, I had to realize a validator for the Swiss Social Security Number, called SV No. (previously AHV No.), in Java. I was searching the WWW for an appropriate implementation but couldn’t find any implementation. So, I implemented the thing myself and decided to share the code, perhaps saving some minutes of time in the busy life of another engineer.
The SV no. is a number that consists of four number blocks which are separated by dots. The first block must consist of three, the next two of four, and the last block of two digits. The last digit is a check sum which is calculated according to the EAN13 algorithm. A detailed description of the number format is given on this page (in German).
Using the present implementation of the Social Security No. Checker is quite easy. Just call
|
|
The first argument number is the SV No. The second argument is the countryCode that must match. If left blank or null, the country code is not checked. If the country code is given, it must match the first block of three digits.
The checking procedure consists of three steps:
- The number of blocks and their length are checked.
- There is a check, whether the blocks consist of digits, or whether they contain non-digits.
- The checksum is computed and checked against the last digit.
The method returns an integer number with one of the following results:
- SSNNumberChecker.ERR_NO: no problem was found within the SV No.
- SSNNumberChecker.ERR_LENGTH: either number blocks are missing, a wrong separator character was used, or one or more blocks do not match the required length.
- SSNNumberChecker.ERR_CONTAINS_NON_DIGITS: there are non-digits in the blocks.
- SSNNumberChecker.ERR_CHECKSUM: the checksum is incorrect, which may indicate a typo in the number.
- SSNNumberChecker.ERR_WRONG_COUNTRY: indicates that the number does not start with the expected country code. This error can only occur when the argument countryCode is given.
The implementation can be found here