Skip to content

Instantly share code, notes, and snippets.

@mhamzas
Created March 27, 2020 09:05
Show Gist options
  • Save mhamzas/d81fd3b250b1062ced01f9dbdb32c798 to your computer and use it in GitHub Desktop.
Save mhamzas/d81fd3b250b1062ced01f9dbdb32c798 to your computer and use it in GitHub Desktop.
Canadian SIN Checksum Checker - Apex Method
// Canadian SIN Checksum Checker
// Source: https://stackoverflow.com/questions/4263398/how-do-i-check-the-validity-of-the-canadian-social-insurance-number-in-c
@AuraEnabled(cacheable=true)
public static boolean IsCanadianSIN(string sSIN){
Integer iChecksum = 0;
Integer iDigit = 0;
// Checking if number is not starting with 9
if(!sSIN.startsWith('9')){
for (Integer i = 0; i < sSIN.length(); i++){
// even number else odd
if ((Math.Mod((i+1),2)) == 0){
iDigit = Integer.Valueof(sSIN.Substring(i, 1))*2;
iChecksum += (iDigit < 10) ? iDigit : iDigit - 9;
} else {
iChecksum += Integer.Valueof(sSIN.Substring(i, 1));
}
}
if(Math.Mod(iChecksum,10) == 0){
return true;
} else {
return false;
}
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment