LiveCode Mobile Development Cookbook
上QQ阅读APP看书,第一时间看更新

Restricting the user input

There is an old saying: garbage in and garbage out. This refers to users entering invalid data into a computerized system. Fortunately, we have some control over what users can input. This recipe shows you how this is done.

How to do it...

To restrict user input, perform the following steps:

  1. Create a new main stack.
  2. Drag a text input box to the stack's card.
  3. Add the following code to the text input box:
    on keyDown theKey
      if theKey is in "abcdefghijklmnopqrstuvwxyz" then
        pass keyDown
      else
         beep
        end if
      end keyDown

How it works...

By evaluating the user's input prior to allowing it to pass to the next level in the message chain, we can selectively accept or reject it.

There's more...

In this recipe, we intercepted messages from the keyboard using our on keyDown handler. In LiveCode, messages are triggered by events such as the mouseDown message being sent when the user selects/clicks on an object. In this example, the event was the clicking of the object and mouseDown was the message. Messages have an informational path in that they are first heard or are available at the object, group (if applicable), card, stack, and finally, at the LiveCode Engine level.