From a19058d9a261d032d464d2ec7a0d409bb6db7421 Mon Sep 17 00:00:00 2001 From: hamed <15238-genius.ha@users.noreply.drupalcode.org> Date: Fri, 19 Jun 2026 11:23:01 +0330 Subject: [PATCH] feat(userAccount): add validation for Iranian national code and update input handling --- .../detailUser/information/ButtonSendData.js | 7 +++++++ .../detailUser/information/form/index.js | 10 +++++----- helper/index.js | 16 +++++++++++++++- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/components/dashboard/userAccount/detailUser/information/ButtonSendData.js b/components/dashboard/userAccount/detailUser/information/ButtonSendData.js index 1b8cbb9..f884b88 100644 --- a/components/dashboard/userAccount/detailUser/information/ButtonSendData.js +++ b/components/dashboard/userAccount/detailUser/information/ButtonSendData.js @@ -2,6 +2,7 @@ import ArrowLeftWhiteD from "@/components/icons/ArrowLeftWhiteD"; import { changeDateType, handleTimeExpiresToken, + isValidIranNationalCode, removeAdditionalKeysDashboard, } from "@/helper"; import { request } from "@/services/response"; @@ -81,6 +82,12 @@ function ButtonSendData({ }; const sendReq = () => { + if (information?.national_code && !isValidIranNationalCode(information.national_code)) { + setErrors((prev) => ({ ...prev, national_code: true })); + toast.error("کد ملی وارد شده معتبر نیست"); + return; + } + setLoading(true); information.prev_data ? patchData() : postData(); diff --git a/components/dashboard/userAccount/detailUser/information/form/index.js b/components/dashboard/userAccount/detailUser/information/form/index.js index f08d075..4233c16 100644 --- a/components/dashboard/userAccount/detailUser/information/form/index.js +++ b/components/dashboard/userAccount/detailUser/information/form/index.js @@ -42,14 +42,14 @@ function Form({ insurance, errors, changeData, information }) { + changeData(val.replace(/\D/g, "").slice(0, 10), name) + } + value={information?.national_code ?? ""} /> diff --git a/helper/index.js b/helper/index.js index 27da1ab..3eb9e57 100644 --- a/helper/index.js +++ b/helper/index.js @@ -272,8 +272,8 @@ export const handleTimeExpiresToken = (expires_in) => { }; export const removeAdditionalKeysDashboard = (information) => { + // فقط متادیتای سمت فرانت حذف می‌شود؛ national_code باید ارسال شود تا قابل ثبت/آپدیت باشد. const { - national_code, changed, created, id, @@ -709,3 +709,17 @@ export function changeNumToDefault(str) { .replace(/[٠-٩]/g, d => "٠١٢٣٤٥٦٧٨٩".indexOf(d)); } + +// اعتبارسنجی کد ملی ایران: ۱۰ رقم + الگوریتم رقم کنترلی. +export const isValidIranNationalCode = (input) => { + if (!input) return false; + const code = String(input).replace(/\D/g, ""); + if (!/^\d{10}$/.test(code)) return false; + // کدهای با ارقام یکسان (مثل 0000000000) نامعتبرند + 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; +};