Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to detect user input(numbers pad) language is English or Persian.
I'm using the following scripts but they work when user OS is Linux, but not when users OS is windows.

What I have tried:

first solution:

JavaScript
document.getElementById("num").addEventListener("keyup", 
          function(e) {
          var patt = new RegExp("^[\u06F0-\u06F9]+$");
          if (patt.test(e.key)) 
            document.getElementById("lang").innerHTML = "Persian";                            
          else 
            document.getElementById("lang").innerHTML = "english";
             })


Second solution:

JavaScript
document.getElementById('num').addEventListener('keypress',function(e){
      if (isPersian(e.charCode))
      document.getElementById("lang").innerHTML='Persian';
      else
      document.getElementById("lang").innerHTML='English';

         });

      function isPersian(charCode){
        return (charCode >= 1776 && charCode <= 1785) 
      }
Posted
Updated 10-Jul-22 3:13am

1 solution

Try this: Navigator language Property[^]
It returns the language the user UI is set to, so it's probably a better way to check input.
 
Share this answer
 
Comments
Hamid01 10-Jul-22 9:21am    
Navigator language property returns browsers language not the user's selected keyboard input layout. @OriginalGriff
OriginalGriff 10-Jul-22 9:54am    
Yes, and that means it's likely to accurately reflect what the user is typing ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900