Store and Display Persian Numbers
Problem:
Please note that Latin/English numbers are as follows:
0123456789 -Latin/English Numbers
۰۱۲۳۴۵N۶۷۸۹ - Persian/Arabic Numbers
In PhpGrid when the language is Persian/Arabic and the numbers are entered in a field, PhpGrid provides an error message saying “Please enter a number” ( see attached PhpGrid Error message image). It is expecting the field entry to be an English number and does not accept the Persian/Arabic number entered. Is there a setting that needs to be SET, so that PhpGrid translates the Persian/Arabic number into English and stores it in the database?
Please let me know what am I missing?
Solution
Store number as integer in database table, but at UI layer, change them to display in persian via custom formatter for retrieve and save (unformatter)
// Sample code to convert perisan number to integerfunction persianToInteger(persianNumber) { return parseInt(persianNumber.replace(/[۰-۹]/g, d => '0123456789'['۰۱۲۳۴۵۶۷۸۹'.indexOf(d)]), 10);}// Example usage:console.log(persianToInteger("۱۲۳۴۵")); // Output: 12345// Sample code to convert integer to persian numberfunction toPersianNumber(number) { const persianDigits = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹']; return number.toString().replace(/\d/g, digit => persianDigits[digit]);}// Example usage:const number = 12345;const persianNumber = toPersianNumber(number);