Accepting only digit characters during data entry
There are times when you might want to limit the type of characters a user is allowed to entry into a field. For example you may only want to allow numbers into a booking reference field. This lesson will show you how to control this.
The keyDown message
The keyDown message is sent whenever a character key is pressed, the message is sent to the focused field. The LiveCode engine must receive the keyDown message in order to respond to a typed character so trapping the message and not passing it prevents the user typing into the field.
The keyDown message is sent with a parameter, pKey, which is the character that was pressed.
The keyDown handler
When the user types into a field the keyDown handler check the pKey parameter to see what was typed. If a number was typed then the handler passes the keyDown message allowing it to proceed along the message path and be added to the field.
The is a operator
The is a operator allows the user to check the type of a value, in this case the keyDown message will check if the pressed key is a number.
The is a operator can accept strings that contain non-digits as numbers. For example, "22.5" is a number even though it contains a period. However the parameter to keyDown is always a single character and for a single character to be a number it must be a digit from 0-9.
The keyDown code
The following handler belongs on the script of the entry field
on keyDown pKey if pKey is not a number then ## If the parameter is not 0-9 ## beep and prevent text entry beep else ## Allow text entry by passing the keyDown message pass keyDown end if end keyDown