|
发表于 2013-12-29 21:43:49
|
显示全部楼层
使用registerFilter即可
例如以下代码只可以输入字母和数字:
--以下代码来自我正在开发的Lua字符串扩展库,详见https://github.com/wtof1996/TI-Lua_String_Library_Extension/
--[[
TI-Lua String Library Extension ---- ctype(non assert version)
Copyright 2013 wtof1996
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
]]--
ctype = {};
--The exception table
ctype.exception = {["invChar"] = "ctype:invalid character", ["invType"] = "ctype:invalid type", ["longString"] = "ctype:the string is too long"};
--This private function is used to check input & convert all kinds of input into number.
function ctype.CheckInput(input)
if(type(input) == "number") then
if((input > 0x7F) or (input < 0) or (math.ceil(input)) ~= input) then return nil, ctype.exception.invChar end;
return input;
elseif(type(input) == "string") then
if(#input > 1) then return nil, ctype.exception.longString end;
local res = input:byte();
if((res > 0x7F) or (res < 0)) then return nil, ctype.exception.invChar end;
return res;
else
return nil, ctype.exception.invType;
end
end
--Public function
function ctype.isalnum(char)
local res, err = ctype.CheckInput(char);
if(err ~= nil) then return nil, err end;
if( ((res >= 0x30) and (res <= 0x39)) or
((res >= 0x41) and (res <= 0x5A)) or
((res >= 0x61) and (res <= 0x7A))
) then return true, nil end;
return false, nil;
end
--EOF
TextBox = D2Editor.newRichText();
TextBox:move(20, 20)
TextBox:resize(100, 100)
TextBox:setBorder(1);
TextBox:registerFilter{
charIn = function (char)
local ret, err = ctype.isalnum(char);
if(err == nil) then
if(ret == true) then return false end; --如果输入的是字母或者数字那么我们允许传递给文本框
end
return true;--否则不允许
end
}
|
|