Last active
March 1, 2022 10:06
-
-
Save lukacat10/50fffbb84b68ee97543a533df9fb79d8 to your computer and use it in GitHub Desktop.
Validate israeli IDs in go
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
package validate_israeli_id_module | |
import ( | |
"regexp" | |
"strconv" | |
"strings" | |
) | |
var( | |
IDSanitizer = regexp.MustCompile(`^[0-9]{1,9}$`) | |
CONST_ONE_TWO = [9]int{1,2,1,2,1,2,1,2,1} | |
) | |
func StringCharsToIntArr(inputString string) []int { | |
var output []int | |
for _, r := range []rune(inputString) { | |
output = append(output, int(r - '0')) | |
} | |
return output | |
} | |
func ValidateID(content string) bool { | |
if !IDSanitizer.MatchString(content) { | |
return false | |
} | |
content_runes := StringCharsToIntArr(strings.Repeat("0", 9 - len(content)) + content) | |
multiplied := StringCharsToIntArr(strings.Repeat("0", 9)) | |
for i := 0; i < 9; i++ { | |
multiplied[i] = content_runes[i] * CONST_ONE_TWO[i] | |
if multiplied[i] > 9 { | |
asstr := StringCharsToIntArr(strconv.Itoa(multiplied[i])) | |
sum_asstr := 0 | |
for _, ru := range asstr { | |
sum_asstr += ru | |
} | |
multiplied[i] = sum_asstr | |
} | |
} | |
summed_multiplied := 0 | |
for _, multiplied_item := range multiplied { | |
summed_multiplied += multiplied_item | |
} | |
if summed_multiplied % 10 == 0 { | |
return true | |
} | |
return false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment