LiveCode字段级输入验证

时间:2013-03-08 15:48:00

标签: validation livecode

在桌面应用程序的某些字段中键入时,如何将用户输入限制为仅数字(允许小数)?

〜罗杰

8 个答案:

答案 0 :(得分:5)

请注意,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

注意 - 如果键入无效字符,则输入光标移动到行的开头;如果你想做任何不同的事情,那么你可以锁屏(作为'textChanged'中的第一个动作)并在你完成后解锁。

答案 1 :(得分:4)

每次用户按下某个键时,都会向您的字段发送一条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

答案 2 :(得分:2)

亚历克斯,我喜欢你的剧本,但它也不包括你的剧本。如果你想输入负数,它就不起作用(如果你在空字段中以“ - ”号开头)。所以下面是更正的脚本,我希望这次覆盖每一个案例,但是如果你输入不允许的字符(那里还有一些东西)它会失去选择。

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

答案 3 :(得分:1)

我发现将此作为单独的答案添加比编辑我之前的答案更容易也可能更好。这需要马雷克的改进,再加上一个领先的案例#34;&#34;并且通常也允许前导和尾随空格,而不是仅在键入数字后。

在尝试键入或粘贴其他字符失败后,它还会恢复选择/插入点(选择无法完全恢复,但插入点应始终在之前的任何选择之后立即完成。

请注意,我们肯定会因为更加完整而易于理解......

我们仍然没有完全涵盖插入科学记数法(例如1.2e34)becuae的情况,在这种情况下最终值是有效的,但其中一个干预状态(1.2e)不是。< / p>

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

答案 4 :(得分:0)

如何使用“是一个数字”?唯一特殊情况是你需要在开头允许“ - ”:

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

答案 5 :(得分:0)

亚历,

如果出现以下情况,您的脚本将无法正常运行:

  1. 在键入开头键入“ - ”符号(当字段为空时 没有任何数字)
  2. 如上所述,当输入无效字符时,输入光标移动到行的开头
  3. 请检查:

    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,但是在接下来的几天里没有时间,所以决定把它放在那里玩某人。

答案 6 :(得分:0)

这实际上比它看起来更棘手。  但这有效:

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

答案 7 :(得分:-2)

这也可以起作用:

on KeyDown pKey    如果pKey是“0123456789”的字符之一。和(我的长度)&lt; 15然后传递keyDown 结束Keydown