feat(representation profile): Iranian national-code + IBAN validation, English digits

- national code: full Iranian checksum (control digit + reject all-same),
  not just length 10 → "کد ملی نامعتبر است" on failure
- IBAN (شبا): validate IR + 24 digits + mod-97; input forces uppercase,
  strips to IR/digits, maxLength 26
- Persian/Arabic digits converted to Latin on input and before validation
  so the field behaves with an English/numeric keyboard

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
hamed
2026-07-11 15:20:08 +03:30
co-authored by Claude Opus 4.8
parent 2f0131171d
commit 1508d38daa
@@ -17,6 +17,35 @@ function toJalali(iso: string): string {
return y && m && d ? `${y}/${m}/${d}` : '';
}
// ارقام فارسی/عربی → لاتین (کیبورد انگلیسی؛ ورودی چسبانده‌شده هم نرمال شود)
function toLatinDigits(s: string): string {
return s.replace(/[۰-۹٠-٩]/g, (d) =>
String('۰۱۲۳۴۵۶۷۸۹٠١٢٣٤٥٦٧٨٩'.indexOf(d) % 10),
);
}
// اعتبارسنجی کد ملی ایران (طول ۱۰ + رقم کنترلی)
function isValidIranNationalCode(code: string): boolean {
if (!/^\d{10}$/.test(code)) return false;
if (/^(\d)\1{9}$/.test(code)) return false; // ارقام یکسان نامعتبر
const check = +code[9];
let sum = 0;
for (let i = 0; i < 9; i++) sum += +code[i] * (10 - i);
const r = sum % 11;
return r < 2 ? check === r : check === 11 - r;
}
// اعتبارسنجی شبای ایران: IR + ۲۴ رقم + کنترل mod-97
function isValidIranIban(raw: string): boolean {
const iban = toLatinDigits(raw).replace(/\s/g, '').toUpperCase();
if (!/^IR\d{24}$/.test(iban)) return false;
const rearranged = iban.slice(4) + iban.slice(0, 4);
const numeric = rearranged.replace(/[A-Z]/g, (c) => String(c.charCodeAt(0) - 55));
let rem = 0;
for (const ch of numeric) rem = (rem * 10 + +ch) % 97;
return rem === 1;
}
interface IbanItem {
id: string;
iban: string;
@@ -86,16 +115,17 @@ export default function RepresentationProfilePage() {
});
const submitNationalCode = () => {
const code = nationalCode.replace(/\D/g, '');
if (code.length !== 10) { toast.error('کد ملی باید ۱۰ رقم باشد'); return; }
const code = toLatinDigits(nationalCode).replace(/\D/g, '');
if (!isValidIranNationalCode(code)) { toast.error('کد ملی نامعتبر است'); return; }
verifyMut.mutate(code);
};
const submitIban = () => {
if (!iban.trim()) { toast.error('شماره شبا را وارد کنید'); return; }
const clean = toLatinDigits(iban).replace(/\s/g, '').toUpperCase();
if (!isValidIranIban(clean)) { toast.error('شماره شبا نامعتبر است (IR + ۲۴ رقم)'); return; }
const jalali = toJalali(birthDate);
if (!jalali) { toast.error('تاریخ تولد را انتخاب کنید'); return; }
addIbanMut.mutate({ iban: iban.trim(), birth_date: jalali });
addIbanMut.mutate({ iban: clean, birth_date: jalali });
};
return (
@@ -127,7 +157,7 @@ export default function RepresentationProfilePage() {
<input
type="text" inputMode="numeric" dir="ltr" maxLength={10} value={nationalCode}
placeholder="کد ملی ۱۰ رقمی"
onChange={(e) => setNationalCode(e.target.value.replace(/\D/g, ''))}
onChange={(e) => setNationalCode(toLatinDigits(e.target.value).replace(/\D/g, ''))}
style={{ width: 220, height: 38, padding: '0 12px', borderRadius: 'var(--r-sm)', border: '1px solid var(--border)', background: 'var(--surface)', color: 'var(--text)', fontSize: 13.5, boxSizing: 'border-box', textAlign: 'center' }}
/>
<button className="btn primary" onClick={submitNationalCode} disabled={verifyMut.isPending}>
@@ -178,8 +208,9 @@ export default function RepresentationProfilePage() {
{verified && ibans.length < 2 && (
<div style={{ display: 'flex', alignItems: 'center', gap: 8, flexWrap: 'wrap', marginTop: 14 }}>
<input
type="text" dir="ltr" value={iban} placeholder="IR000000000000000000000000"
onChange={(e) => setIban(e.target.value)}
type="text" inputMode="numeric" dir="ltr" maxLength={26} value={iban}
placeholder="IR000000000000000000000000"
onChange={(e) => setIban(toLatinDigits(e.target.value).toUpperCase().replace(/[^IR0-9]/g, ''))}
style={{ width: 320, height: 38, padding: '0 12px', borderRadius: 'var(--r-sm)', border: '1px solid var(--border)', background: 'var(--surface)', color: 'var(--text)', fontSize: 13, boxSizing: 'border-box', fontFamily: 'monospace' }}
/>
<PersianDatePicker