document.addEventListener('DOMContentLoaded', function() {
// Assuming all your textareas have class 'number-input'
const textareas = document.querySelectorAll('.number-input');
textareas.forEach(function(textarea) {
// Initialize Bootstrap tooltip for this textarea
// Assuming Bootstrap and jQuery are included in your project
$(textarea).tooltip({
trigger: 'manual'
});
// Add keydown event listener to restrict input
textarea.addEventListener('keydown', function(e) {
// Allow backspace, delete, tab, escape, enter, arrows
if (
e.key === 'Backspace' || e.key === 'Delete' || e.key === 'Tab' ||
e.key === 'Escape' || e.key === 'Enter' ||
e.key === 'ArrowLeft' || e.key === 'ArrowRight' ||
e.key === 'ArrowUp' || e.key === 'ArrowDown'
) {
return;
}
// Allow Ctrl+A, Ctrl+C, Ctrl+V, Ctrl+X
if ((e.ctrlKey || e.metaKey) && (
e.key === 'a' || e.key === 'c' || e.key === 'v' || e.key === 'x'
)) {
return;
}
const persianDigits = /[۰-۹]/;
const englishDigits = /[0-9]/;
if (!persianDigits.test(e.key) && !englishDigits.test(e.key)) {
e.preventDefault();
// Show tooltip for 3 seconds
const tooltipMessage = "فقط میتوان عدد وارد کرد";
$(textarea).attr('data-original-title', tooltipMessage).tooltip('show');
setTimeout(function() {
$(textarea).tooltip('hide');
}, 3000);
}
});
// Add input event listener to format the number
textarea.addEventListener('input', function() {
let value = textarea.value;
// Remove non-digit characters
value = value.replace(/[^0-9۰-۹]/g, '');
// Convert Persian digits to English digits
const persianDigits = ['۰','۱','۲','۳','۴','۵','۶','۷','۸','۹'];
const englishDigits = ['0','1','2','3','4','5','6','7','8','9'];
for (let i = 0; i < persianDigits.length; i++) {
value = value.replace(new RegExp(persianDigits[i], 'g'), englishDigits[i]);
}
// Add commas every three digits
value = value.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
// Convert back to Persian digits if needed
if (/[۰-۹]/.test(textarea.value)) {
for (let i = 0; i < englishDigits.length; i++) {
value = value.replace(new RegExp(englishDigits[i], 'g'), persianDigits[i]);
}
}
textarea.value = value;
});
// On focus, remove currency and commas
textarea.addEventListener('focus', function() {
let value = textarea.value;
// Remove currency
value = value.replace(/\s*(ریال|تومان)$/, '');
// Remove commas
value = value.replace(/,/g, '');
textarea.value = value;
});
// On blur, add currency
textarea.addEventListener('blur', function() {
let value = textarea.value;
// Remove existing currency
value = value.replace(/\s*(ریال|تومان)$/, '');
if (value !== '') {
// Assuming you have a way to get the selected currency
const currency = getSelectedCurrency(); // Implement this function
textarea.value = value + ' ' + currency;
}
});
});
// Function to get selected currency
// You need to implement this based on your application's logic
function getSelectedCurrency() {
// For example, get the value from a dropdown with id 'currency-select'
const currencySelect = document.getElementById('currency-select');
if (currencySelect) {
return currencySelect.value;
} else {
// Default currency
return 'تومان';
}
}
});
Explanation:
- Keydown Event Listener: Prevents any non-digit characters from being entered. If an invalid character is detected, it shows a Bootstrap tooltip for 3 seconds notifying the user that only numbers are allowed.
- Input Event Listener: Formats the input value by removing any non-digit characters, converting Persian digits to English for formatting, adding commas every three digits, and then converting back to Persian digits if necessary.
- Focus and Blur Event Listeners: On focus, it strips the currency and commas for easy editing. On blur, it appends the selected currency (
ریال or تومان) to the value.
- Bootstrap Tooltip: Utilizes Bootstrap's tooltip functionality to display messages to the user. Make sure Bootstrap and jQuery are properly included in your project.
- Currency Selection: The
getSelectedCurrency function should be implemented based on how your application handles currency selection.
Note: Adjust the selectors and implement the getSelectedCurrency function according to your application's specific setup.