# Scripting numeric text field component

**URL:** <https://forum.inductiveautomation.com/t/scripting-numeric-text-field-component/85560>\
**Category:** Ignition\
**Tags:** ignition81, scripting, vision\
**Created:** [February 12, 2024, 4:00pm UTC](https://forum.inductiveautomation.com/t/scripting-numeric-text-field-component/85560 "2024-02-12T16:00:08Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Om33](https://avatars.discourse-cdn.com/v4/letter/o/7ba0ec/32.png) [@Om33](https://forum.inductiveautomation.com/u/Om33)\
**Post date:** [February 12, 2024, 4:00pm UTC](https://forum.inductiveautomation.com/t/scripting-numeric-text-field-component/85560/1 "2024-02-12T16:00:08Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [February 13, 2024, 5:55pm UTC](https://forum.inductiveautomation.com/t/scripting-numeric-text-field-component/85560/2 "2024-02-13T17:55:53Z")

</div>

Consider using a Formatted Text Field, instead.

This example uses a regex pattern up to 6 digits.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/8/68263f76233f728f15fa210d2667740b3bc0b12f.png)

---

<div class="post-metadata">

**Author:** ![Om33](https://avatars.discourse-cdn.com/v4/letter/o/7ba0ec/32.png) [@Om33](https://forum.inductiveautomation.com/u/Om33)\
**Post date:** [February 14, 2024, 6:28am UTC](https://forum.inductiveautomation.com/t/scripting-numeric-text-field-component/85560/3 "2024-02-14T06:28:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Om33](https://avatars.discourse-cdn.com/v4/letter/o/7ba0ec/32.png) [@Om33](https://forum.inductiveautomation.com/u/Om33)\
**Post date:** [February 14, 2024, 7:00am UTC](https://forum.inductiveautomation.com/t/scripting-numeric-text-field-component/85560/4 "2024-02-14T07:00:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [February 14, 2024, 8:26pm UTC](https://forum.inductiveautomation.com/t/scripting-numeric-text-field-component/85560/5 "2024-02-14T20:26:52Z")

</div>

You may find this of interest.

> [@Formatted Text field - committed value](https://forum.inductiveautomation.com/t/formatted-text-field-committed-value/39936/6):
>
> @MMaynard, [Test\_Window.zip](https://forum.inductiveautomation.com/uploads/short-url/gFVmlpDj9VVlrSycevO9g8uVRlU.zip) (3.9 KB) So this is really interesting. I created the attached and did some more testing. Formatted Field 1 was placed in the window, configured with a regular expression pattern and it worked. I then deleted the regular expression pattern and it continues to work with that field blank. Formatted field 2, is configured as placed in the window and it does not work. Even if you Allow invalid text, it will not commit nor can you directly change the committed value…

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

---

<div class="post-metadata">

**Author:** ![Om33](https://avatars.discourse-cdn.com/v4/letter/o/7ba0ec/32.png) [@Om33](https://forum.inductiveautomation.com/u/Om33)\
**Post date:** [February 15, 2024, 5:49am UTC](https://forum.inductiveautomation.com/t/scripting-numeric-text-field-component/85560/6 "2024-02-15T05:49:39Z")

</div>

I have seen this. Maybe someone else could comment

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [February 15, 2024, 7:30am UTC](https://forum.inductiveautomation.com/t/scripting-numeric-text-field-component/85560/7 "2024-02-15T07:30:47Z")

</div>

> [@Om33](#):
>
> commit the value using "Enter"

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:**

```auto
# 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](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/5/9526a91b91aa2d8a508b0607e917f1d8a4dc4a73.png)

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

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

```

---

<div class="post-metadata">

**Author:** ![Om33](https://avatars.discourse-cdn.com/v4/letter/o/7ba0ec/32.png) [@Om33](https://forum.inductiveautomation.com/u/Om33)\
**Post date:** [February 15, 2024, 1:43pm UTC](https://forum.inductiveautomation.com/t/scripting-numeric-text-field-component/85560/8 "2024-02-15T13:43:27Z")

</div>

Thank you, will try it out

---

<div class="post-metadata">

**Author:** ![Om33](https://avatars.discourse-cdn.com/v4/letter/o/7ba0ec/32.png) [@Om33](https://forum.inductiveautomation.com/u/Om33)\
**Post date:** [February 16, 2024, 10:06am UTC](https://forum.inductiveautomation.com/t/scripting-numeric-text-field-component/85560/9 "2024-02-16T10:06:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [February 16, 2024, 10:27am UTC](https://forum.inductiveautomation.com/t/scripting-numeric-text-field-component/85560/10 "2024-02-16T10:27:20Z")

</div>

It works for me. Do you have a formatted mask pattern set?  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/8/b89116c1884386fee00f0a8af3a59ef8eb0b8544.png)

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

**Result:**  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/1/d1b001510d347534030a9558f8e98d3317b2299e.png)

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/b/0b875b1986d03bcfdb3bcfc81ba865023be40006.png)

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](https://www.docs.inductiveautomation.com/docs/8.1/appendix/components/vision-components/input/formatted-text-field)

---

<div class="post-metadata">

**Author:** ![Om33](https://avatars.discourse-cdn.com/v4/letter/o/7ba0ec/32.png) [@Om33](https://forum.inductiveautomation.com/u/Om33)\
**Post date:** [February 16, 2024, 3:29pm UTC](https://forum.inductiveautomation.com/t/scripting-numeric-text-field-component/85560/11 "2024-02-16T15:29:11Z")

</div>

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.
