basics · 2025-03-17 · 6 min read

Formatting card numbers as the user types — without breaking the caret

Grouping digits 4-4-4-4 (or 4-6-5 for Amex) cuts typos dramatically — if you do it without wrecking cursor position, paste or password-manager autofill.

Mirror the grouping printed on the card.
Mirror the grouping printed on the card. — © Kindel Media · Pexels

Users read numbers in the groups printed on the card, so mirror that grouping in the input. Visa and Mastercard use 4-4-4-4; American Express uses 4-6-5; 14-digit Diners Club uses 4-6-4. The grouping has to switch live as the detected brand changes — type 37 then 42 in the inspection bench and watch the spacing adapt.

State vs. display

Keep the stored value digits-only and derive the formatted string for display. Validation, Luhn checks and tokenization all operate on the raw digits; only the input shows spaces.

Preserving the caret

Re-setting an input's value moves the cursor to the end, which makes editing in the middle impossible. Before formatting, count how many digits sit left of the caret; after formatting, place the caret after that many digits. Handle the end-of-string case separately so appending stays snappy.

Mobile keyboards and autofill

  • inputmode="numeric" opens the number pad on phones without forcing type="number" (which strips leading zeros and breaks grouping) — see MDN on inputmode.
  • autocomplete="cc-number", cc-name, cc-exp, cc-csc let browsers and password managers fill the whole form in one tap.
  • Strip non-digits on paste so '4242-4242-4242-4242' or numbers with spaces work.
  • Set maxlength to 23 (19 digits + 4 separators), not 16.

Google's payment form best practices cover the full checklist. Pair good formatting with helpful error messages for the biggest conversion win.

Keep digits-only state; format only for display.
Keep digits-only state; format only for display.

Keywords: card number input, format credit card input, digit grouping, caret position, inputmode numeric, autocomplete cc-number, payment form UX

Frequently asked questions

Should a card number field use type=number?

No. Use type=text with inputmode=numeric; type=number strips leading zeros and prevents grouping with spaces.

How are Amex numbers grouped?

4-6-5 (e.g. 3782 822463 10005), unlike the 4-4-4-4 pattern of Visa and Mastercard.

On this site

External references

Related articles

تنسيق رقم البطاقة أثناء الكتابة — دون إفساد مؤشر الكتابة — النسخة العربية

تجميع الخانات 4-4-4-4 (أو 4-6-5 لأمكس) يقلّل الأخطاء كثيرًا — إن فعلته دون تخريب موضع المؤشر أو اللصق أو التعبئة التلقائية لمديري كلمات المرور.

يقرأ المستخدمون الأرقام بالمجموعات المطبوعة على البطاقة، فاعكس التجميع نفسه في الحقل. فيزا وماستركارد 4-4-4-4؛ أمريكان إكسبريس 4-6-5؛ داينرز كلوب ذات 14 خانة 4-6-4. ويجب أن يتبدّل التجميع حيًّا مع تغيّر العلامة المكتشَفة — اكتب 37 ثم 42 في منضدة الفحص وراقب التباعد يتكيّف.

الحالة مقابل العرض

احتفظ بالقيمة المخزّنة أرقامًا فقط واشتق السلسلة المنسّقة للعرض. التحقق وفحوص لون والترميز كلها تعمل على الخانات الخام؛ والحقل وحده يعرض المسافات.

الحفاظ على المؤشر

إعادة ضبط قيمة الحقل تنقل المؤشر إلى النهاية، ما يجعل التعديل في المنتصف مستحيلًا. قبل التنسيق احسب عدد الخانات على يسار المؤشر؛ وبعده ضع المؤشر بعد هذا العدد من الخانات. وعالج حالة نهاية السلسلة منفصلة ليبقى الإلحاق سريعًا.

لوحات مفاتيح الجوال والتعبئة التلقائية

  • ‏inputmode="numeric" يفتح لوحة الأرقام على الهواتف دون إجبار type="number" (الذي يحذف الأصفار الأولى ويكسر التجميع) — انظر MDN عن inputmode.
  • ‏autocomplete="cc-number" وcc-name وcc-exp وcc-csc تتيح للمتصفحات ومديري كلمات المرور ملء النموذج كاملًا بنقرة.
  • احذف غير الأرقام عند اللصق كي تعمل '4242-4242-4242-4242' أو الأرقام ذات المسافات.
  • اضبط maxlength على 23 (19 خانة + 4 فواصل) لا 16.

أفضل ممارسات نماذج الدفع من Google تغطي القائمة كاملة. اقرن التنسيق الجيد بـرسائل خطأ مفيدة لأكبر مكسب في التحويل.

أسئلة شائعة

هل يستخدم حقل رقم البطاقة type=number؟

لا. استخدم type=text مع inputmode=numeric؛ فـtype=number يحذف الأصفار الأولى ويمنع التجميع بالمسافات.

كيف تُجمَّع أرقام أمكس؟

4-6-5 (مثل 3782 822463 10005)، بخلاف نمط 4-4-4-4 لفيزا وماستركارد.

Educational content — not legal or financial advice. Open the interactive Check Digit Lab →