开发者

How can I test if an input field contains foreign characters?

I have an input field in a form. Upon pushing submit, I want to validate to make sure the user entered non-latin characters only, so any foreign language characters, like Chinese among many others. Or at the very least test to make sure it does not contain any latin characters.

Could I use a regular expression for this? What would be the best approach for this?

I am validating in both javaScript and in PHP. What solutions can I use to check for foreign characters in the input field in both programm开发者_如何学Pythoning languages?


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.


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");
}


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 not
  • a-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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜