将输入限制为指定语言 [英] Restrict input to a specified language

查看:30
本文介绍了将输入限制为指定语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Flex 4.5 的 TextInput 组件输入一些英文文本.我使用 restrict 属性来...将键盘输入限制为仅字符 a-zA-Z.问题是,如果我用另一种语言复制/粘贴一个单词,我可以将它粘贴到 TextInput 组件中.有没有办法避免这种情况?如果不是,我如何根据指定的语言验证输入?

I use a TextInput component of Flex 4.5 to enter some text in English. I use the restrict attribute to ... restrict the keyboard input to characters a-zA-Z only. The problem is that if i copy/paste a word in another language, i can then paste it into the TextInput component. Is there a way to avoid that? If no, how can i validate the input against a specified language?

我发现中文+语言符号的unicode集是\u4E00到\u9FFF.所以我写了以下内容:

I found out that the unicode set of Chinese+ language symbols is \u4E00 to \u9FFF. So i write the following:

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

但是如果我在 TextInput 中输入hello"这个词,那么它就会验证......错误是什么?

But if i type in the TextInput the word 'hello' then it validates...What is the error?

由于我不能(我的错?还是错误?)在 RegExp 中使用 unicode range,我编写了以下函数来检查某个单词是否为中文,仅此而已.

Since i cannot (my fault? or a bug?) use unicode range with RegExp, i wrote the following function to check if a word is in Chinese and that's it.

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

推荐答案

String.match() 方法返回一个数组,该数组将始终测试为真,即使它为空(请参阅此处的文档:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/String.html#match%28%29)

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

改用 RegExp.test() 方法来看看它是否匹配:

Use the RegExp.test() method instead 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");
}

您还需要检查字符范围 - 我无法让它与正则表达式中的 \u9FFF 匹配.

You'll need to check the character ranges too - I couldn't get it to match with \u9FFF in the regex.

这篇关于将输入限制为指定语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆