Detecting non-European characters
I need to prevent users from entering non-european characters in a textbox.
For example, here's how I disable 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, etc.
I just want to allow:
- ASCII English, standard characters
- Italian accented letters: à è ì ò ù é é ó ó
- other special characters from European languages: French, German ...
Is there a way to do this with ranges?
I tried it /[\u0400-\u04FF]/i
, but it just allows ASCII English (not Italian for example) to be used.
+3
source to share
2 answers
Just allow Unicode characters in the given range / s for example.
/^[a-z\u00C0-\u00F6\u00F8-\u017E]+$/i
Sample script: https://jsfiddle.net/4y6e6bj5/3/
This regex allows basic latin / latin extended A (diacritics and accented letters). It excludes any other character / character.
If you need to allow other specific unicode characters, look at the unicode table and insert as many ranges as you need into the regex
+5
source to share