Skip to content

fix(isCreditCard): anchor Mastercard pattern to 16 digits#2794

Open
hong4rc wants to merge 1 commit into
validatorjs:masterfrom
hong4rc:fix/mastercard-anchor
Open

fix(isCreditCard): anchor Mastercard pattern to 16 digits#2794
hong4rc wants to merge 1 commit into
validatorjs:masterfrom
hong4rc:fix/mastercard-anchor

Conversation

@hong4rc

@hong4rc hong4rc commented Jun 30, 2026

Copy link
Copy Markdown

isCreditCard('5108') returns true, and so does a 25-digit 5100000000000000000000003 — both wrongly accepted as Mastercard.

In the Mastercard pattern, | has the lowest precedence, so the 5x branch ^5[1-5][0-9]{2} is anchored only at the start with no length and no $ — any string that merely starts with a 51–55 prefix matches, regardless of length (Luhn doesn't bound length). Grouping the prefixes so the shared [0-9]{12}$ applies to both branches fixes it:

- mastercard: /^5[1-5][0-9]{2}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}$/,
+ mastercard: /^(5[1-5][0-9]{2}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720))[0-9]{12}$/,

Tests: 5105105105105100 stays valid; 5108 and the 25-digit case are now rejected. npm test green.

@codecov

codecov Bot commented Jun 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (a38f15b) to head (c34b341).

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #2794   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          114       114           
  Lines         2587      2587           
  Branches       656       656           
=========================================
  Hits          2587      2587           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@hong4rc hong4rc force-pushed the fix/mastercard-anchor branch from e807004 to c34b341 Compare June 30, 2026 12:53
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.

1 participant