Limiting the number of characters during data entry
There are times when you might want to limit the number of characters a user can enter into a field. This lesson will show you how to control this.
The keyDown message
The keyDown handler
When the user types into the field the keyDown handler checks to see if the field already contains the maximum number of character allowed. If the field already contains the maximum number of characters LiveCode beeps and doesn't pass the keyDown message which causes the key press to be lost.
The me keyword
The keyword me refers to the object whose script contains the handler that is executing. In this case me refers to the field because the keyDown handler is in the field's script. When a field is referenced in this way LiveCode returns the contents of the field, the length of that content is the number of characters it contains. So the expression
the length of me
is the number of characters in the field.
The keyDown code
The following handler belongs on the script of the entry field
on keyDown pKey if the length of me >= 5 then // If there are 5 or more characters in the field // beep and prevent text entry beep else // Allow text entry by passing the keyDown message pass keyDown end if end keyDown