Open hic003cih opened 4 years ago
/* 從右邊開始,每2的倍數,就乘以2,如果超過9,則將數字減去9,然後所有數字的合可以整除10,驗證通過
4539 1488 0343 6467 -> 4_3_1_8_0_4_6_6_ ->8569 2478 0383 3437
8+5+6+9+2+4+7+8+0+3+8+3+3+4+3+7 = 80 -> 80/10=0
*/
func Valid(input string) bool {
sum := 0
input = strings.Replace(input, " ", "", -1)
inputSlice := strings.Split(input, "")
if len(inputSlice) <= 1 {
return false
}
//fmt.Println(inputSlice)
for i := 0; i < len(inputSlice); i++ {
if i%2 != 0 {
temp, _ := multiply(inputSlice[i])
inputSlice[i] = strconv.Itoa(temp)
}
temp2, _ := strconv.Atoi(inputSlice[i])
fmt.Println(temp2)
sum += temp2
}
fmt.Println(inputSlice)
fmt.Println(sum)
if sum%10 == 0 {
return true
}
return false
}
func multiply(input string) (int, error) {
doubleInput, err := strconv.Atoi(input)
if err != nil {
return 0, errors.New("input is not int")
}
doubleInput = doubleInput * 2
if doubleInput > 9 {
doubleInput = doubleInput - 9
}
return doubleInput, nil
}
Luhn 簡單的驗證身份證號碼的公式,用來驗證身分證和信用卡 a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers and Canadian Social Insurance Numbers.
從右邊開始,每2的倍數,就乘以2,如果超過9,則將數字減去9,然後所有數字的合可以整除10,驗證通過
4539 1488 0343 6467 -> 4_3_1_8_0_4_66 ->8569 2478 0383 3437
8+5+6+9+2+4+7+8+0+3+8+3+3+4+3+7 = 80 -> 80/10=0