最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to identify what keyboard language is with jquery - Stack Overflow

programmeradmin2浏览0评论

is there any way to identify the keyboard language? i have a simple textbox. that i want to check keyboard language. when the user click on textbox if the keyboard language was Contrary to Persian the user can't type anything and show the error:" change your keyboard language to persian" and when the keyboard language changed user can type.

is there any way to identify the keyboard language? i have a simple textbox. that i want to check keyboard language. when the user click on textbox if the keyboard language was Contrary to Persian the user can't type anything and show the error:" change your keyboard language to persian" and when the keyboard language changed user can type.

Share Improve this question asked Oct 2, 2016 at 12:22 inazinaz 1,7755 gold badges22 silver badges43 bronze badges 1
  • I don't think so, but you can use the keydown event to prevent a user from inputting certain characters. In this case you could check and only allow Persian characters. There's tons of examples of this for allowing only numbers if you search on Google. – solarc Commented Oct 2, 2016 at 12:32
Add a comment  | 

5 Answers 5

Reset to default 15

I have used two different approaches for detecting 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"/>

I don't think that's possible - anyway it's probably not what you want (Caps Lock, for example, will still output English) I'd recommend placing a keypress event listener on your textarea and checking each letter against a "Persian only" regex like this (untested):

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

If you want to match ZERO WIDTH SPACE you should add this too:

\u200B

So

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

for more characters like ” | « | » | ?| ; | : | ... see Regex Persian Language

using 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);
}

This:

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

or you can use this: http://imanmh.github.io/persianRex/

发布评论

评论列表(0)

  1. 暂无评论