pkmntrade.club/staticfiles/admin/fields/thousand_sep_field.js

22 lines
No EOL
738 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function convertPersianToEnglish(value) {
const persianNumbers = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
const englishNumbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
for (let i = 0; i < persianNumbers.length; i++) {
value = value.replace(new RegExp(persianNumbers[i], 'g'), englishNumbers[i]);
}
return value;
}
function formatNumber(input) {
let value = convertPersianToEnglish(input.value);
// Remove any existing thousand separators and non-digit characters
value = value.replace(/,/g, '').replace(/\D/g, '');
// Add thousand separators
value = value.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
// Update the input value
input.value = value;
}