javascriptregexlatin

Detecting non european characters


I need to prevent my users from entering non-european characters in a text box.

For example, here's how I disallow Cyrillic:

$('.test').keyup(function(e) {
        var toTest = $(this).val();
        var rforeign = /[\u0400-\u04FF]/i;
        if (rforeign.test(toTest)) {
            alert("No cyrillic allowed");
            $(this).val('');
        } 
    });

But I also need to exclude Arabic, Japanese, and so on.

I just want to allow:

Is there a way to do that with ranges?

I tried /[\u0400-\u04FF]/i but it just allows ASCII English (not Italian for example).


Solution

  • Just allow unicode symbols in some given ranges, e.g.

    /^[a-z\u00C0-\u00F6\u00F8-\u017E]+$/i
    

    Example fiddle: https://jsfiddle.net/4y6e6bj5/3/


    This regular expression allows basic latin / latin extended A (diacritics and accented letters). It excludes any other alphabet/symbol.

    If you need to allow other specific unicode symbols, look at the unicode table and insert as many ranges as you need into the regular expression