Scripting numeric text field component

Hello,

I want to realise following functionality with a numeric text field:
users should enter an ID ( only digits). After entering an ID, pressing Enter should confirm the input. In the keyPressed event script, I want to display relevant information based on the entered ID. However, I'm facing challenges in retrieving the entered ID and preventing users from entering invalid characters.

Could someone help?

Consider using a Formatted Text Field, instead.

This example uses a regex pattern up to 6 digits.

3 Likes

Thank you, using the validation mode "Formatted Mask" solved all my problems . I don't even need the regex pattern

Now having the problem that I can not commit the value using "Enter". How can the user commit a new value?

You may find this of interest.

@steven.cox, did this end up as your solution?

I have seen this. Maybe someone else could comment

I'm not sure I understand what you mean by commit the value, but you can use the keyPressed or keyReleased event handlers to do things on specific keystrokes.

Example:

# Perform a task with text property on enter pressed
if event.keyCode == event.VK_ENTER:
	print "Commit '{}' in some way".format(event.source.text)

Result for 45678 on enter being pressed:
image

Edit: The formatted text field does have a commitEdit method, so perhaps this would work:

# Commit the edit on enter pressed
if event.keyCode == event.VK_ENTER:
	event.source.commitEdit()

Thank you, will try it out

Didn't work, seems like the commitEdit() method is not available

It works for me. Do you have a formatted mask pattern set?
image

This mask requires a 6 digit numerical value. Anything else will throw an exception when enter is pressed using the commitEdit() method.

Result:
image

image

Here is the current documentation for the formatted text field component. There could be some clues for your use case in there:
https://www.docs.inductiveautomation.com/docs/8.1/appendix/components/vision-components/input/formatted-text-field

Thank you, it works now. If you enter the exact number of digits set in the Formatted Mask Pattern, it automatically commits. When the length is not equal, it adds spaces.