Issue
I have an input field with type number, my issue is when using Firefox and safari with Arabic keyboard the number are written ٨٦٥ like that, how can I convert this format to 8754 (English format) while the user typing in the filed? Or i prevent it from typing non English format.
Solution
The following function will change the field key pressed value of the numbers 0123456789 to the Arabic-Eastern form "٠١٢٣٤٥٦٧٨٩".
if you type 1 it will show ١,
if you type 2 it will show ٢, and so on.
It will not affect the other characters.
It can be improved.
document.getElementById('myTextFieldId').addEventListener("keypress", function(e){
let code=e.keyCode-48;
if (code>=0 && code<10) {
e.target.value = e.target.value.slice(0,e.target.selectionStart)
+ "٠١٢٣٤٥٦٧٨٩"[code]
+ e.target.value.slice(e.target.selectionEnd);
e.target.selectionStart = e.target.selectionEnd = e.target.selectionStart + 1;
e.preventDefault();
}
})
<input type="text" id="myTextFieldId" />
Answered By - Mohsen Alyafei
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.