How to determine which language on a keyboard using jquery

Is there a way to identify the keyboard language? I have a simple text box. that I want to check the keyboard language. when the user clicks on the text field if the keyboard language was the opposite. The Persian user cannot enter anything and show an error: "change the language of your keyboard to Persian" and when the keyboard language is changed, the user can enter.

+5
source share
5 answers

I used two different approaches to detect English and Persian characters in my solution:

document.getElementById('a').addEventListener('keypress',function(e){
     if (isEnglish(e.charCode))
       console.log('English');
     else if(isPersian(e.key))
       console.log('Persian');
     else
       console.log('Others')
});

function isEnglish(charCode){
   return (charCode >= 97 && charCode <= 122) 
          || (charCode>=65 && charCode<=90);
}

function isPersian(key){
    var p = /^[\u0600-\u06FF\s]+$/;    
    return p.test(key) && key!=' ';
}
<input id="a" type="text"/>
Run codeHide result
+8
source

, - , , , (Caps Lock, , ) keypress " " ():

document.getElementById('a').addEventListener('keypress',function(e){
     if (e.charCode > 160) 
     console.log('persian');
     else
     console.log('english');
});
<input type="text" id="a"/>
Hide result
+2

ZERO WIDTH SPACE, :

\u200B

,

var p = /^[\u0600-\u06FF\u200B\s]+$/;

, "|" | "|? |; |: |... .

+1

:

function only_persian(str){
    var p = /^[\u0600-\u06FF\s]+$/;
  if (!p.test(str)) {
        alert("not format");
    }
}

: http://imanmh.imtqy.com/persianRex/

0

jquery:

$("#selector").keyup(function (event) {
    var code = event.key.charCodeAt(0);
    if (isEnglish(code)) {
        alert("you typed in english");
        return;
    }
    else {
        alert("please type in english");
    }
});

function isEnglish(charCode) {
    return (charCode >= 97 && charCode <= 122)
        || (charCode >= 65 && charCode <= 90);
}
0

Source: https://habr.com/ru/post/1656513/


All Articles