How Can I Test If An Input Field Contains Foreign Characters?
Solution 1:
In PHP, you can check the Unicode property IsLatin
. That's probably closest to what you want.
So if preg_match('/\p{Latin}/u', $subject)
returns true, then there is at least one Latin character in your $subject
. See also this reference.
JavaScript doesn't support this; you'd have to contruct the valid Unicode ranges manually.
Solution 2:
In Javascript, at least, you can use hex codes inside character range expressions:
var rlatins = /[\u0000-\u007f]/;
You can then test to see if there are any latin characters in a string like this:
if (rlatins.test(someString)) {
alert("ROMANI ITE DOMUM");
}
Solution 3:
You're trying to check if all letters are not Latin, but you do accept accented letters.
A simple solution is to validate the string using the regex (this is useful if you have a validation plugin):
/^[^a-z]+$/i
^...$
- Match from start to end^[...]
- characters that are nota-z
- A though Z,+
- with at least one letter/i
- ignoring case (could also done/^[^a-zA-Z]+$/
)
Another option is simply to look for a letter:
/[a-z]/i
This regex will match if the string conatins a letter, so you can unvalidated it.
In JavaScript you can check that easily with if
:
var s = "שלום עולם";
if(s.match(/^[^a-z]+$/i){
}
or
if(!s.match(/[a-z]/i))
PHP has a different syntax and more security than JavaScript, but the regular expressions are the same.
Post a Comment for "How Can I Test If An Input Field Contains Foreign Characters?"