I have a form in LiveCode that gathers customer details. One of those fields is for their phone number. How do I stop them typing anything other than valid phone number characters?
2 Answers
Neil's answer above is a great simple way of validating for numbers. There are two other methods you might find useful.
Note: Not 'passing' the keyDown message stops it from moving up the message path. The LiveCode field object gets the message last, so if you don't pass it, it won't be added to the field. It's a great way of performing filtering like Neil suggested.
Filter by checking if character is in a list:
on keyDown pKey
if pKey is among the characters of "0123456789()+" then pass keyDown
end keyDown
Filter using a regular expression match:
on keyDown pKey
put me & pKey into tPhoneNumber
if matchText(tPhoneNumber,"[0-9]") then pass keyDown
end keyDown
In the above example you take the current contents and of the field, add the new character and check if it matches the regular expression. Using this method you can use complex regular expressions that force a certain format, length etc.

- 910
- 1
- 6
- 14
Like all LiveCode objects, fields receive various messages with one of them being the keyDown message. If keyDown is present in a field, it will be triggered and passed with a parameter of the current key pressed. As we now know what the key is, we can respond appropriately. -
on keyDown pKey
if pKey is not a number then
answer "you must type numbers only in this field"
else
pass keyDown
end if
end keyDown

- 681
- 3
- 6