2

I know the JavaScript that restricts entering some special characters or number, etc using charcode and ascii value.

Is there anyway that I can block people entering any other language (I mean alphabet) expect English? More specifically how can I block people entering Chinese or some other non English alphabet name in the textbox and make them strictly enter only English alphabet name?

Thanks

Karthik Malla
  • 5,570
  • 12
  • 46
  • 89

3 Answers3

2

I use this function to remove special characters from input

function removeSpecials(evt) {
var input = document.getElementById("myinput");
var patt = /[^\u0000-\u007F ]+/;
setTimeout(function() {
     var value = input.value;
     input.value = value.replace(patt,"");
 },100);

}

The point is: I create a pattern to detect all special characters

var patt = /[^\u0000-\u007F ]+/;;

and I replace the input value

value.replace(patt,"");

Here is a FIDDLE

nanndoj
  • 6,580
  • 7
  • 30
  • 42
  • This code is supposed to remove only form last typed char. I will change the example to remove all from the input. You have to use replace instead – nanndoj Jan 26 '15 at 01:16
1

Easiest way to block all non-ascii inputs would probably be

/^[\x00-\xff]+$/.test(input);

This expression will return true if input is composed of purely ascii characters.

It will return false if any non-ascii character is found.


You can adjust the range of accepted ascii characters to meet your needs. \x00-\x55 includes ascii char codes 0-255

Mulan
  • 129,518
  • 31
  • 228
  • 259
0

You can easily restrict the user to a specific character set by checking their input with something like inputString.match(/^[A-Za-z]+$/). That will catch languages like Chinese, though there are plenty of languages which share almost the same alphabet as English and thus cannot be easily blocked.

Ixrec
  • 966
  • 1
  • 7
  • 20
  • Thanks mate, no problem. My main goal is to block languages like Chinese, Japanese, etc. which doesn't follow English alphabet. – Karthik Malla Jan 26 '15 at 00:56