在桌面应用程序的某些字段中键入时,如何将用户输入限制为仅数字(允许小数)?
〜罗杰
请注意,本的回答并不涵盖所有情况
你可以做所有这些
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
亚历克斯,我喜欢你的剧本,但它也没有涵盖所有情况。如果您想输入负数(如果您在空白字段中以“-”符号开头),则它不起作用。所以下面是更正的脚本,我希望这次涵盖所有情况,但是如果您输入不允许的字符,它会丢失选择(还有一些事情要做)。
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
我发现将其添加为单独的答案比编辑我之前的答案更容易并且可能更好。这需要 Marek 的改进,增加了一个领先的“。”。并且通常还允许前导和尾随空格,而不是仅在输入数字之后。
它还会在尝试键入或粘贴其他字符失败后恢复选择/插入点(选择无法完全恢复,但插入点应始终在任何先前选择之后立即完成。
请注意,我们肯定会以易于理解为代价换取更多的完整性......
我们仍然没有完全涵盖插入科学记数法(例如 1.2e34)的情况,因为在这种情况下最终值是有效的,但中间状态之一(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
使用“是一个数字”怎么样?唯一的特殊情况是您需要在开头允许“-”:
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
亚历克斯,
如果出现以下情况,您的脚本将无法正常工作:
请检查:
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 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
这也可以工作:
如果 pKey 在“0123456789”字符中,则在 KeyDown pKey 上。并且(我的长度)< 15 然后通过 keyDown end Keydown