I have a input field of type number
. I want to accept Persian/Arabic numbers as well in this field. I wanted to convert these numbers to English using jQuery, but it seems I can't retrieve these values.
jQuery('input[type="number"]').on('input', function() {
console.log(jQuery(this).val());
});
For non-English numbers val()
is empty string. Is there any way to get the value of a number input field, if the value is not a English number?
UPDATE:
Persian numbers: ۰ ۱ ۲ ۳ ۴ ۵ ۶ ۷ ۸ ۹
English numbers: 0 1 2 3 4 5 6 7 8 9
UPDATE2:
Looks like Firefox returns the correct val
but Chrome returns empty string. So my question is applicable on Google Chrome.
you can use this function:
function toEnglishNumber(strNum) {
var pn = ["۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"];
var en = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
var an = ["٠", "١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩"];
var cache = strNum;
for (var i = 0; i < 10; i++) {
var regex_fa = new RegExp(pn[i], 'g');
var regex_ar = new RegExp(an[i], 'g');
cache = cache.replace(regex_fa, en[i]);
cache = cache.replace(regex_ar, en[i]);
}
return cache;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With