Skip to content

Commit

Permalink
fix: index out of range in examples/credit-card-form when ccn is empty (
Browse files Browse the repository at this point in the history
  • Loading branch information
grafviktor authored Jun 29, 2023
1 parent cd63c32 commit c1b0b19
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions examples/credit-card-form/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ func ccnValidator(s string) error {
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
// case it should be a space
if len(s)%5 == 0 && s[len(s)-1] != ' ' {
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
c := strings.ReplaceAll(s, " ", "")
Expand Down

0 comments on commit c1b0b19

Please sign in to comment.