Restricting user input

I am working on a form with a number of input boxes. These are populated from a SQL database when the form is opened and can then be edited by the user before the database is updated with the changes.

The data to be entered can be a value from 0 to 99, or left blank. I tried using a Numeric Text Field, but this doesn’t allow a blank entry. I looked at using a Text Field, but this would require examining the key pressed and only allowing numeric or editing keys. It also allows any number of characters to be entered - the ‘Maximum Characters’ property only seems to be used to verify what the user entered when the focus moves to another control.

The Formatted Text Field seems similar in that it allows any data to be entered, then throws it away if it didn’t match the regular expression.

I would like to be able to restrict users to only pressing the appropriate keys and not exceeding the maximum length (in this case 2 characters).

Any ideas how to achieve this?

Well, I seem to have come up with a solution, although it takes fairly careful setting up.

First, I created a Text Field, then set its ‘Defer Updates’ property to false so it fires a ‘text’ property update on every key press. The ‘Maximum Characters’ property is left at -1, otherwise the string comparison in the script doesn’t work correctly. Finally I set the ‘Reject Updates During Edit’ property (an expert property) to false, otherwise you can’t reset the value of the Text Field from the script.

I then entered the following script under the Text Field’s propertyChange event:if event.propertyName == 'text': if (not event.newValue.isdigit() and event.newValue != '') or (len(event.newValue) > 2): event.source.text = event.oldValue If the new character entered is not a number (excluding the possibility of a blank entry) or its length is longer than 2 characters, the value of the Text Field is reset to the value before the character was entered.

If anyone has a more straightforward solution, please let me know :slight_smile:

The final episode in this gripping monologue: the previous approach seemed to work, but adding an animation to change the background colour when a user made a change exposed a problem: trying to set the ‘text’ property in the event code handling a change in the ‘text’ property meant that animations were being processed in the wrong order - the background was being changed even though the text was being reset to its old value. To solve this, Carl suggested the following code:if event.propertyName == 'text': if (not event.newValue.isdigit() and event.newValue != '') or (len(event.newValue) > 2): def doItLater(evt=event): evt.source.text = evt.oldValue fpmi.system.invokeLater(doItLater) This change means that the ‘text’ property is not changed until the event code has completed.

Although this worked, any incorrect character briefly appeared in the Text Field before its value was reset. Carl then suggested that the Formatted Text Field would work better. This was set up with a Regular Expression Pattern of ‘\d?\d?’ which allows 2 (optional) integers to be entered. To make sure the user could not enter any invalid characters, the ‘Allows Invalid Text’ property was set to false, and changes were picked up immediately by setting the ‘Commit While Typing’ to true.

The final result is a user input box that allows the user to enter a 1 or 2 digit integer or leave it blank as required, and that changes the background when a user changes the value.

Al - Thanks for typing this up! I was going to do it, but it slipped my mind.