javascript - Restricting Multilanguages for input field -


i have got task restrict input field non english languages.only english should enter on field.

my textbox is

<input type="text"/> 

the function is

 $(document).on("keypress", "input[type='text'] function (event) {         return suppressnoneng(event);     });  function suppressnoneng(eventkey) {     var key = eventkey.which || eventkey.keycode;     if (key > 128) { sefalert("only english allowed"); return false; }     else { return true; } } 

its worked in case of chinese,greek , other also.but in case of spanish,french, not working because same ascii character used in english , french. there solution problem?please help

fiddle

its pretty simple. need match every character entered regex checks whether character entered english alphabet, or not.

$("#mytextbox").on("keypress", function(event) {     var englishalphabetandwhitespace = /[a-za-z ]/g;     var key = string.fromcharcode(event.which);     if (englishalphabetandwhitespace.test(key)) {         return true;     }     alert ("this not in english");//put message here!!! }); 

after comments:

every key on keyboard has keycode. when press key e, computer interpret keycode (69, in case). it's difficult make computer understand difference between french e or english e.

if dont want alert user, replace alert return false;.

if need detect browser language:

use this:

var userlang = navigator.language || navigator.userlanguage;  alert ("the language using is: " + userlang); if(userlang!=whatever-you-want){     alert("only whatever-you-want allowed!!!") } 

check language


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -