Search code examples
javascriptvalidationasciifromcharcode

Block typing in any other alphabet than english


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


Solution

  • 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