LiveCode LessonsLiveCode LessonsHow To - LiveCode Sample ScriptsTextAccepting only digit characters during data entry

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.

If a non-numeric character was pressed LiveCode beeps to warn the user and doesn't pass the keyDown message, which causes the key press to be lost.

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

6 Comments

Trevor

I see the keyDown command for accepting digits but how do you enter decimal point e.g 24.75?

Hanson Schmidt-Cornelius

Hi Trevor,

try something like the following:

on keyDown pKey
if pKey is a number or pKey is "." then
## Allow text entry by passing the keyDown message
pass keyDown
else
## If the parameter is not 0-9
## beep and prevent text entry
beep
end if
end keyDown

This will allow you to enter digits and full stops. If you want to allow only one full stop to be entered, then you need to keep a counter of how many full stops were entered or a switch that indicates that a full stop was entered. You then have to update the condition to take the value of the counter or switch into account.

Kind Regards,

Hanson

carol

I'm taking a test and I need to know what sort of things cannot be controlled during data entry

Elanor Buchanan

Hi Carol

You can use in if statement to check for almost anything, so if you only want vowels for example you can use

if tKey is among the items of "a,e,i,o,u"

You can also use the matchText function to check against a regular expression.

I hope that helps.

Elanor

Graeme

Is there a way to make sure what is entered is unique?

Elanor Buchanan

Hi Graeme

You can handle the keyDown message and use an if statement to check if the character is already in the field. Something like

on keyDown pKey
if pKey is in field "entry" then
## If the character is already in the field
## beep and prevent text entry
beep
else
## Allow text entry by passing the keyDown message
pass keyDown
end if
end keyDown

I hope that helps.

Kind regards

Elanor

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.