fix: index out of range in examples/credit-card-form when ccn is empty (#770)

This commit is contained in:
Roman Leonenkov 2023-06-30 00:37:48 +01:00 committed by GitHub
parent cd63c32c73
commit c1b0b19d64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 3 deletions

View File

@ -53,14 +53,15 @@ func ccnValidator(s string) error {
return fmt.Errorf("CCN is too long") return fmt.Errorf("CCN is too long")
} }
if len(s) == 0 || len(s)%5 != 0 && (s[len(s)-1] < '0' || s[len(s)-1] > '9') {
return fmt.Errorf("CCN is invalid")
}
// The last digit should be a number unless it is a multiple of 4 in which // The last digit should be a number unless it is a multiple of 4 in which
// case it should be a space // case it should be a space
if len(s)%5 == 0 && s[len(s)-1] != ' ' { if len(s)%5 == 0 && s[len(s)-1] != ' ' {
return fmt.Errorf("CCN must separate groups with spaces") return fmt.Errorf("CCN must separate groups with spaces")
} }
if len(s)%5 != 0 && (s[len(s)-1] < '0' || s[len(s)-1] > '9') {
return fmt.Errorf("CCN is invalid")
}
// The remaining digits should be integers // The remaining digits should be integers
c := strings.ReplaceAll(s, " ", "") c := strings.ReplaceAll(s, " ", "")