fix persian number in input

This commit is contained in:
2026-08-14 21:05:11 +03:30
parent 82dc6b82d8
commit 2b27b16296
10 changed files with 160 additions and 123 deletions
@@ -41,8 +41,8 @@
:place-holder="amountPlaceholder"
@onchange="onAmountChange"
/>
<div v-if="isMoney && Number(normalizedAmount) > 10" class="wallet-charge-form__hint">
معادل {{ usePrice(Number(normalizedAmount) / 10, true, 'تومان') }}
<div v-if="isMoney && normalizedAmount > 10" class="wallet-charge-form__hint">
معادل {{ usePrice(normalizedAmount / 10, true, 'تومان') }}
</div>
</a-form-item>
@@ -58,6 +58,10 @@ import type { FormInstance } from 'ant-design-vue'
import type { Charge } from '@/model/wallet'
import { inputRules } from '@/utils/inputRules'
import { usePrice } from '@core/utils/usePrice'
import {
normalizeAmountRaw,
parseAmount,
} from '@/pages/menuPages/customers/base/utils/walletChargeForm'
const props = withDefaults(
defineProps<{
@@ -124,35 +128,16 @@ const notePlaceholder = computed(() =>
isDischarge.value ? 'دلیل کسر موجودی' : 'دلیل افزایش موجودی',
)
/** ارقام فارسی/عربی → لاتین؛ جداکننده هزار حذف؛ ممیز یکسان */
function normalizeAmountRaw(raw: string | number | undefined | null): string {
return String(raw ?? '')
.trim()
.replace(/[۰-۹]/g, (d) => String('۰۱۲۳۴۵۶۷۸۹'.indexOf(d)))
.replace(/[٠-٩]/g, (d) => String('٠١٢٣٤٥٦٧٨٩'.indexOf(d)))
.replace(/[٬,\s]/g, '')
.replace(/٫/g, '.')
}
function hasDecimalSeparator(raw: string): boolean {
// بعد از حذف جداکننده هزار، نقطه = اعشار
return raw.includes('.')
}
function parseAmount(raw: string | number | undefined | null): number {
const normalized = normalizeAmountRaw(raw)
if (!normalized) return 0
const num = Number(normalized)
return Number.isFinite(num) ? Math.abs(num) : NaN
}
const normalizedAmount = computed(() => parseAmount(props.modelValue.amountAbs))
function onAmountChange(raw: string | number) {
if (!requiresInteger.value) return
const text = normalizeAmountRaw(raw)
if (!hasDecimalSeparator(text)) return
// قطع اعشار — فقط بخش صحیح بماند
const whole = text.split('.')[0] ?? ''
props.modelValue.amountAbs = whole === '' ? undefined : whole
}
@@ -1,4 +1,15 @@
import type { Charge, WalletChargeDirection } from '@/model/wallet'
import { parseNumericValue, sanitizeNumericString } from '@core/utils/convert'
/** ارقام فارسی/عربی → لاتین؛ جداکننده هزار حذف؛ ممیز یکسان */
export function normalizeAmountRaw(raw: string | number | undefined | null): string {
return sanitizeNumericString(raw, { allowDecimal: true })
}
export function parseAmount(raw: string | number | undefined | null): number {
const n = parseNumericValue(raw)
return Number.isFinite(n) ? Math.abs(n) : 0
}
export function createEmptyChargeForm(customerId?: number): Charge {
return {
@@ -13,7 +24,7 @@ export function createEmptyChargeForm(customerId?: number): Charge {
}
export function buildChargePayload(form: Charge, productId?: number | null): Charge {
const amount = Math.abs(Number(form.amountAbs) || 0)
const amount = parseAmount(form.amountAbs)
const signed = form.direction === 'discharge' ? -amount : amount
const payload: Charge = {
value: signed,