【发布时间】:2013-02-24 05:26:10
【问题描述】:
在桌面应用程序的某些字段中输入时,如何限制用户输入仅为数字(允许小数)?
~罗杰
【问题讨论】:
-
您还没有定义任何语言或技术,是 .Net 还是 java?
-
Roger,你也需要处理拖放和粘贴吗?
标签: validation livecode
在桌面应用程序的某些字段中输入时,如何限制用户输入仅为数字(允许小数)?
~罗杰
【问题讨论】:
标签: validation livecode
这实际上比看起来更棘手。 但这有效:
on keyDown pKey
if pKey = "-" and me <> "" then exit keyDown
if pKey = "." and "." is in me then exit keyDown
if pKey is in "-0123456789." then pass keydown
end keyDown
【讨论】:
这也可以:
在 KeyDown pKey 上 如果 pKey 在“0123456789”的字符中。并且(我的长度)
【讨论】:
亚历克斯,
如果出现以下情况,您的脚本将无法正常工作:
请检查:
local sExisting, sSelected, sSelected1
on openField
put the text of me into sExisting
end openField
on selectionChanged
put the selectedChunk into sSelected1
put "sSelected1: " & sSelected1 into line 1 of msg
end selectionChanged
on arrowKey
send "selectionChanged" to me in 0
pass arrowKey
end arrowKey
on textChanged
local tMinus, tDot
put empty into tDot
put empty into tMinus
--
put the selectedChunk into sSelected
put "sSelected: " & sSelected into line 2 of msg
--
get matchChunk (me, "^(-?)[0-9]*(\.)?([0-9]*)$", tMinus, tMinus, tDot, tDot)
if it then
switch tDot
case 2
if tMinus = 1 then put "-0." & char 3 to -1 of me into me
put (word 2 of sSelected) + 1 into word 2 of sSelected
put (word 4 of sSelected) + 1 into word 4 of sSelected
break
case 1
put "0." & char 2 to -1 of me into me
put (word 2 of sSelected) + 1 into word 2 of sSelected
put (word 4 of sSelected) + 1 into word 4 of sSelected
break
default
end switch
send "selectionChanged" to me in 0
put me into sExisting
else
if not (me is empty) then
put sExisting into me
put sSelected1 into sSelected
end if
end if
select sSelected
end textChanged
它可能并不完美,但粘贴后的选择和任何字符输入(甚至不允许)都应该是正确的。您可以从“-”或“.”开始输入。此外,使用“自动更正” - 如果您输入“.34”或“-.34”,您将被更正为“0.34”或“-0.34”。
您可以通过箭头更改光标位置,粘贴不允许的字符串后不会更改光标位置。粘贴正确的字符串后,新的光标位置在粘贴字符串的末尾。
我不喜欢的已知故障缺少删除前导零(可能也应该删除尾随)。
如果您发现一些错误,请告诉我,以便我尝试修复它(很高兴)。 脚本有点长,昨天看到了一些bug,不过这几天没时间,所以决定放在那里让别人玩。
【讨论】:
如何使用“是一个数字”?唯一的特殊情况是您需要在开头允许“-”:
on KeyDown theKey
if (the text of me & theKey) is a number then
pass keyDown
else if the text of me is empty and theKey is "-" then
pass keyDown
end if
end KeyDown
【讨论】:
我发现将其添加为单独的答案比编辑我之前的答案更容易并且可能更好。这需要 Marek 的改进,增加了一个领先的“。”。并且通常还允许前导和尾随空格,而不是仅在输入数字之后。
它还会在尝试键入或粘贴其他字符失败后恢复选择/插入点(选择无法完全恢复,但插入点应始终在任何先前选择后立即完成。
请注意,我们肯定会以易于理解为代价换取更完整...
我们仍然没有完全涵盖插入科学记数法(例如 1.2e34)的情况,因为在这种情况下 final 值是有效的,但中间状态之一(1.2e)不是。
local sExisting
on openfield
put the text of me into sExisting
end openfield
on textChanged
local tMe, tChunk, tDeltaLength, tWhere
put the selectedChunk of me into tChunk
put word 1 to -1 of the text of me into tMe -- strip leading/trailing spaces
if tMe is a number or tMe = "-" or tMe = "." or tMe is empty then
put the text of me into sExisting
else
-- calculate how many characters were inserted,
-- and move the insertion point back to there
put the number of chars in me - the number of chars in sExisting into tDeltaLength
put sExisting into me
put word 4 of tChunk - tDeltaLength into tWhere
put tWhere into word 4 of tChunk
put tWhere into word 2 of tChunk
do ("select after " & tChunk & " of me")
end if
end textChanged
【讨论】:
Alex,我喜欢你的脚本,但它并没有涵盖所有情况。如果您想输入负数(如果您在空白字段中以“-”符号开头),则它不起作用。所以下面是更正的脚本,我希望这次涵盖所有情况,但是如果您输入不允许的字符,它会丢失选择(还有一些事情要做)。
local sExisting, sSelected
on openfield
put the text of me into sExisting
end openfield
on textChanged
--put the selectedText && sExisting && the text of me into msg
put the selectedChunk into sSelected
if the text of me is a number or me is empty or me is "-" then
put the text of me into sExisting
else
put sExisting into me
end if
select sSelected
end textChanged
【讨论】:
or me is "." 添加到 if 测试中即可轻松包含。处理选择/插入更难。我会更新我的答案以涵盖这一点。
请注意,Ben 的回答并未涵盖所有情况
所有这些你都可以用
local sExisting
on openfield
put the text of me into sExisting
end openfield
on textChanged
put the selectedText && sExisting && the text of me into msg
if the text of me is a number or me is empty then
put the text of me into sExisting
else
put sExisting into me
end if
end textChanged
NB - 如果你输入了一个无效字符,输入光标会移动到行首;如果您想做任何不同的事情,那么您可以锁定屏幕(作为“textChanged”中的第一个操作)并在完成后解锁。
【讨论】:
每次用户按键时,都会向您的字段发送 keydown 消息:
on keydown pKey
-- check the input --
-- pass on the keydown message --
end keydown
添加以下代码以检查键入的键是否为数字“。”并且该字段尚未包含“.”:
on keydown pKey
if pKey is a number then
pass keydown
else if pKey is "." then
set the itemdel to "."
if the number of items of me < 2 then
pass keydown
end if
end if
end keydown
【讨论】: