Limit input to the specified language

I am using the TextInput Flex 4.5 component to input some English text. I use the restrict attribute to ... restrict keyboard input to a-zA-Z characters only. The problem is that if I copy / paste a word into another language, I can paste it into the TextInput component. Is there any way to avoid this? If not, how can I check the input in the specified language?

I found out that the Unicode character set with Chinese + languages ​​is \ u4E00 to \ u9FFF. Therefore, I write the following:

var chRE:RegExp = new RegExp("[\u4E00-\u9FFF]", "g");
if (inputTI.text.match(chRE)) {
  trace("chinese");
}
else {
  trace("other");
}

But if I enter the word "hello" in TextInput, then it checks ... What is the error?

Since I cannot (my mistake? Or mistake?), Use the Unicode range with RegExp, I wrote the following function to check if there is a word in Chinese and what it is.

private function isChinese(word:String):Boolean
{
    var wlength:int = word.length;
    for (var i:int = 0; i < wlength; i++) {
            var charCode:Number = word.charCodeAt(i);
            if (charCode <= 0x4E00 || charCode >= 0x9FFF) {
                    return false;
        }
    }
    return true;
}
+1
source share
1 answer

The method String.match()returns an array that will always check true even if it is empty (see here docs: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/String.html#match%28%29 )

Use the method RegExp.test()to see if it matches:

// Check your character ranges:
//var chRE:RegExp = new RegExp("[\u4E00-\u9FFF]", "g"); // \u9FFF is unrecognised and iscausing issues.
var chRE:RegExp = new RegExp("[\u4E00]+", "g"); // This works.
if (chRE.test(inputTI.text)) {
  trace("chinese");
}
else {
  trace("other");
}

You also need to check character ranges - I couldn't get it to match \ u9FFF in regex.

0
source

All Articles