Skip to content

Instantly share code, notes, and snippets.

@canhlinh
Last active December 15, 2021 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save canhlinh/0aaa7019a7e2e58d7787e218982ff684 to your computer and use it in GitHub Desktop.
Save canhlinh/0aaa7019a7e2e58d7787e218982ff684 to your computer and use it in GitHub Desktop.
validate NRIC golang
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