It uses the modulo 10 (Luhn) algorithm to check that the organisation number is valid, is a company / person, is a male or is a co ordination number.
Last active
March 30, 2020 10:42
-
-
Save leon/3238f1aa0ae2bbb6a2b5 to your computer and use it in GitHub Desktop.
Swedish Organisation number / SSN validator
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
package util; | |
import org.apache.commons.lang3.StringUtils; | |
public class IdNumberValidator { | |
// Invalid Result is used as a return value when the test fails | |
private static final IdNumberValidatorResult invalid = new IdNumberValidatorResult(); | |
public static IdNumberValidatorResult validate(String value) { | |
// Check for nulls and false lengths | |
if (value == null || StringUtils.IsEmpty(value) || value.length() < 10) { | |
return invalid; | |
} | |
try { | |
// Remove dash and plus | |
value = value.trim().replace("-", "").replace("+", ""); | |
String century = ""; | |
// Remove century and check number | |
if (value.length() == 12) { | |
century = value.substring(0, 2); | |
value = value.substring(2, 12); | |
} else if (value.length() == 10) { | |
value = value.substring(0, 10); | |
} else { | |
return invalid; | |
} | |
// Remove check number | |
int check = Integer.parseInt(value.substring(9, 10)); | |
String sValue = value.substring(0, 9); | |
int result = 0; | |
// Calculate check number | |
for (int i = 0, len = sValue.length(); i < len; i++) { | |
int tmp = Integer.parseInt(sValue.substring(i, i+1)); | |
if ((i % 2) == 0) { | |
tmp = (tmp * 2); | |
} | |
if (tmp > 9) { | |
result += (1 + (tmp % 10)); | |
} else { | |
result += tmp; | |
} | |
} | |
boolean isValid = (((check + result) % 10) == 0); | |
boolean isSSN = Integer.parseInt(value.substring(2, 4), 10) < 13 && Integer.parseInt(value.substring(4, 6), 10) < 32; | |
boolean isCoOrdinationNumber = Integer.parseInt(value.substring(4, 6), 10) > 60; | |
boolean isMale = !((Integer.parseInt(value.substring(8, 9)) % 2) == 0); | |
boolean isCompany = Integer.parseInt(value.substring(2, 4), 10) >= 20; | |
return new IdNumberValidatorResult(isSSN ? century + value : value, isValid, isSSN, isCoOrdinationNumber, isMale, isCompany); | |
} catch (NumberFormatException ex) { | |
ex.printStackTrace(); | |
} | |
return invalid; | |
} | |
} |
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
package util; | |
public class IdNumberValidatorResult { | |
private String idno = null; | |
private boolean valid = false; | |
private boolean ssn = false; | |
private boolean coOrdinationNumber = false; | |
private boolean male = true; | |
private boolean company = false; | |
public String getIdno() { | |
return idno; | |
} | |
public boolean isValid() { | |
return valid; | |
} | |
public boolean isSSN() { return ssn; } | |
public boolean isCoOrdinationNumber() { return coOrdinationNumber; } | |
public boolean isMale() { | |
return male; | |
} | |
public boolean isCompany() { | |
return company; | |
} | |
public IdNumberValidatorResult() {} | |
public IdNumberValidatorResult(String idno, boolean valid, boolean ssn, boolean coOrdinationNumber, boolean male, boolean company) { | |
this.idno = idno; | |
this.valid = valid; | |
this.ssn = ssn; | |
this.coOrdinationNumber = coOrdinationNumber; | |
this.male = male; | |
this.company = company; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very awesome and usefull! Thanks!