diff --git a/assets/admin/pages/RepresentationProfilePage.tsx b/assets/admin/pages/RepresentationProfilePage.tsx index 594f4298..837cf2e6 100644 --- a/assets/admin/pages/RepresentationProfilePage.tsx +++ b/assets/admin/pages/RepresentationProfilePage.tsx @@ -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() { 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' }} />