Skip to content

Accept hyphen grouping in credit_card validation - #1626

Open
official-burak wants to merge 1 commit into
go-playground:masterfrom
official-burak:fix/credit-card-hyphen-grouping
Open

official-burak wants to merge 1 commit into
go-playground:masterfrom
official-burak:fix/credit-card-hyphen-grouping

Conversation

@official-burak

Copy link
Copy Markdown

Fixes Or Enhances

credit_card already splits on spaces before the Luhn check, so 4624 7482 3324 9780 is valid, but the same digits with hyphens are not. Hyphens are now treated as the same grouping separator, so Visa 4-4-4-4 and Amex 4-6-5 work. Double hyphens still fail, matching the existing double-space rule.

Fixes #1624

Make sure that you've checked the boxes below before you submit PR:

  • Tests exist or have been written that cover this particular change.

@go-playground/validator-maintainers

Spaces were already stripped before the Luhn check, so a number like 4624 7482 3324 9780 passed, but the same digits with hyphens did not. Hyphens are now treated as the same grouping separator. Double hyphens still fail, matching the existing double-space rule.

Fixes go-playground#1624
@official-burak
official-burak requested a review from a team as a code owner September 8, 2026 17:43
Comment thread baked_in.go
val := fl.Field().String()
// Hyphens are a common grouping separator (Visa 4-4-4-4, Amex 4-6-5).
// Treat them like spaces so the existing segment rules still apply.
val := strings.ReplaceAll(fl.Field().String(), "-", " ")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using strings.ReplaceAll, strings.Split, bytes.Buffer, and strings.Split(..., "") creates multiple unnecessary string and slice allocations.

It would be nice to see a version which processes the string in a single pass, allocates memory only once, validates that only digits/spaces/hyphens are present and then uses Luhn checker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Should credit_card validator support dash-separated card numbers?

2 participants