主题:  对文本框的输入如何进行限制

三仙半

职务:普通成员
等级:1
金币:0.0
发贴:241
注册:2006/1/24 15:46:21
#12006/6/17 20:10:43
我现在想要限制一个文本框的输入:要求输入的数字必须能转化为数字(正负均可);输入的范围是受到限制的(比如+85~-85之间);小数位数也是受到限制的(比如2位);必须是一个单行文本框(也就是“回车”时结束输入,而不是换行)。
各位大侠,请给予指点。


闭起眼睛看人生

三仙半

职务:普通成员
等级:1
金币:0.0
发贴:241
注册:2006/1/24 15:46:21
#22006/6/17 21:19:48
十万火急,自己顶!


闭起眼睛看人生

Super ChiCk

职务:版主
等级:5
金币:15.0
发贴:3502
注册:2003/9/6 14:53:41
#32006/6/19 15:49:31
property pKeyList
on beginsprite me
pKeyList=[36,82, 83, 84, 85, 86, 87, 88, 89, 91, 92, 65, 29, 25, 28, 26, 22, 23, 21, 20, 19, 18, 47, 51]
end

on keydown me
if pKeyList.getone(_key.keycode )then
pass
end if
end


输入的范围是受到限制的(比如+85~-85之间);小数位数也是受到限制的(比如2位)
这些在输入后按确定 然后检测比较好 实时检测也没什么意义



三仙半

职务:普通成员
等级:1
金币:0.0
发贴:241
注册:2006/1/24 15:46:21
#42006/6/24 13:05:42
我是在模拟一个软件,它是这样实现的,我是没有办法呀!
我做的是这样的:
on keyDown
-- alert the key
case the keyCode of:
-- case the key of
82,83,84,85,86,87,88,89,91,92:
--小键盘数字0-9
checkInput("inputBox",the key,-85,85,2)
18,19,20,21,23,22,26,28,25,29:
--大键盘数字9-0
checkInput("inputBox",the key,-85,85,2)
27,24,47:
--大键盘上的负号,正号,小数点
checkInput("inputBox",the key,-85,85,2)
70,78,65:
--小键盘上的负号,正号,小数点
checkInput("inputBox",the key,-85,85,2)
51:
--退格键
bkSpace("inputBox")
36:
--回车,用于执行确认输入程序
alert("回车啦!")
end case
end
--判断成员memName的文本连接inputChar后转化得到的数字,
--是否大于等于lBound并小于等于rBound,同时要保证小数位数不大于decimal
on checkInput memName,inputChar,lBound,rBound,decimal
--确定成员memName的文本长度
memTextLength = member(memName).text.length
--确定小数点的位置,如不为0,则证明该数字中已经有小数点
decimalPositon = offset(".", member(memName).text)
--处理正号、负号:正号、负号只能出现在数字的第一个字符处——即memTextLength=0
if inputChar = "=" then
inputChar = "+"
end if
if (inputChar = "-" or inputChar = "+") and memTextLength<>0 then
beep
exit
end if
--处理小数点:小数点前至少有一位数字,且只能有一个小数点;如果decimal=0,则不允许接受小数点
if inputChar = "." and (decimal=0 or decimalPositon<>0 or memTextLength=0 or (memTextLength<>0 and "0123456789" contains the last char of member(memName).text=false)) then
beep
exit
end if
--处理数字“0”
if inputChar = "0" and memTextLength=0 then
beep
exit
end if
--处理小数位数
if decimalPositon <> 0 and decimalPositon = memTextLength-2 then
beep
exit
end if
if member("inputBox").text&the key.length = 1 and "-+" contains the key then
put the key after member("inputBox")
exit
end if
--判断连接inputChar后得到的数字是否超过界限
if float(member("inputBox").text&the key)>rBound or float(member("inputBox").text&the key)<lBound then
beep
exit
end if
put inputChar after member("inputBox")
end
--删除成员memName的最后一个字符,如果为“空”则Beep
on bkSpace memName
if member(memName).text.length<>0 then
member(memName).text = chars(member(memName).text,1,member(memName).text.length-1)
else
beep
end if
end

请各位仁兄帮忙指正。看看有比这个更简单合理的做法吗?


闭起眼睛看人生

三仙半

职务:普通成员
等级:1
金币:0.0
发贴:241
注册:2006/1/24 15:46:21
#52006/7/11 19:26:20
我做的还有问题,比如首位是0,后面还可以输入任何数字,如果做限制的话,又会多次响起beep,大家帮看看,我现在懵了。


闭起眼睛看人生

古剑刀

职务:普通成员
等级:1
金币:0.0
发贴:41
注册:2004/6/24 20:55:05
#62006/8/11 16:11:41
如果你听楼上这位兄弟的话,这些在输入后按确定 然后检测比较好 实时检测也没什么意义