Last active
December 15, 2021 07:46
-
-
Save canhlinh/0aaa7019a7e2e58d7787e218982ff684 to your computer and use it in GitHub Desktop.
validate NRIC golang
This file contains 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
var RegexNRIC = regexp.MustCompile(`^(S|T|F|G)\d{7}[A-Z]$`) | |
func ValidateNRIC(s string) bool { | |
nric := strings.ToUpper(s) | |
if !RegexNRIC.Match([]byte(nric)) { | |
return false | |
} | |
weight := 0 | |
n, _ := strconv.Atoi(string(nric[1])) | |
weight += n * 2 | |
n, _ = strconv.Atoi(string(nric[2])) | |
weight += n * 7 | |
n, _ = strconv.Atoi(string(nric[3])) | |
weight += n * 6 | |
n, _ = strconv.Atoi(string(nric[4])) | |
weight += n * 5 | |
n, _ = strconv.Atoi(string(nric[5])) | |
weight += n * 4 | |
n, _ = strconv.Atoi(string(nric[6])) | |
weight += n * 3 | |
n, _ = strconv.Atoi(string(nric[7])) | |
weight += n * 2 | |
if nric[0] == 'T' || nric[0] == 'G' { | |
weight += 4 | |
} | |
rorate := weight % 11 | |
if nric[0] == 'S' || nric[0] == 'T' { | |
return nric[8] == st[rorate] | |
} | |
if nric[0] == 'F' || nric[0] == 'G' { | |
return nric[8] == fg[rorate] | |
} | |
return false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment