# Perspective: Limit text input field to regex pattern?

**URL:** <https://forum.inductiveautomation.com/t/perspective-limit-text-input-field-to-regex-pattern/38936>\
**Category:** Ignition\
**Tags:** ignition80, perspective\
**Created:** [September 22, 2020, 1:47pm UTC](https://forum.inductiveautomation.com/t/perspective-limit-text-input-field-to-regex-pattern/38936 "2020-09-22T13:47:21Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [September 22, 2020, 1:47pm UTC](https://forum.inductiveautomation.com/t/perspective-limit-text-input-field-to-regex-pattern/38936/1 "2020-09-22T13:47:21Z")

</div>

v8.1.0

I want to let the user enter either blank or a number. I can’t use the numeric entry field as it doesn’t allow blanks, so I next option is to use a text input limited to numeric characters. Can I do this?

---

<div class="post-metadata">

**Author:** ![zacht](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@zacht](https://forum.inductiveautomation.com/u/zacht)\
**Post date:** [September 22, 2020, 2:24pm UTC](https://forum.inductiveautomation.com/t/perspective-limit-text-input-field-to-regex-pattern/38936/2 "2020-09-22T14:24:02Z")

</div>

I don’t know of anything built-in, but you could turn off defer updates and use a property change script similar to here ([Limit Characters in a Textbox when Typing](http://forum.inductiveautomation.com/t/limit-characters-in-a-textbox-when-typing/29372/2)).

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [September 22, 2020, 2:26pm UTC](https://forum.inductiveautomation.com/t/perspective-limit-text-input-field-to-regex-pattern/38936/3 "2020-09-22T14:26:00Z")

</div>

Scratch what I said, I didn’t see the defer updates there… I’ll give it a go, cheers!

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [September 22, 2020, 2:45pm UTC](https://forum.inductiveautomation.com/t/perspective-limit-text-input-field-to-regex-pattern/38936/4 "2020-09-22T14:45:11Z")

</div>

Well… it sort of works. It works technically, but practically it’s deceptive. While the `props.text` property doesn’t contain non-numerics, the text displayed in the input field itself still does ☹

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

Also, this is my change script on the `props.text` property:

```auto
	import re
	pattern = '[^0-9]' # remove text that matches this pattern
	text = currentValue.value
	res = re.search(pattern, text)
	
	if res is not None:
		text = re.sub(pattern, '', text)
		self.props.text = text

```

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [September 22, 2020, 2:49pm UTC](https://forum.inductiveautomation.com/t/perspective-limit-text-input-field-to-regex-pattern/38936/6 "2020-09-22T14:49:31Z")

</div>

`re.sub` substitutes all non-numeric (all characters that match the pattern) characters for no characters (effectively removing them)

---

<div class="post-metadata">

**Author:** ![zacht](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@zacht](https://forum.inductiveautomation.com/u/zacht)\
**Post date:** [September 22, 2020, 2:50pm UTC](https://forum.inductiveautomation.com/t/perspective-limit-text-input-field-to-regex-pattern/38936/7 "2020-09-22T14:50:06Z")

</div>

Yeah as soon as I posted I realized I hadn’t read all the way to the bottom. My bad.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [September 22, 2020, 2:51pm UTC](https://forum.inductiveautomation.com/t/perspective-limit-text-input-field-to-regex-pattern/38936/8 "2020-09-22T14:51:44Z")

</div>

Really I could have just used re.sub without search, but I figured it was better not to keep writing to the field every time the user types a character 🧐

---

<div class="post-metadata">

**Author:** ![zacht](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@zacht](https://forum.inductiveautomation.com/u/zacht)\
**Post date:** [September 22, 2020, 3:01pm UTC](https://forum.inductiveautomation.com/t/perspective-limit-text-input-field-to-regex-pattern/38936/9 "2020-09-22T15:01:19Z")

</div>

I tried your script in a TextField component and it works for me in properties and in display. I tried it in designer and a client. The non-numeric will show up but it disappears almost instantly. This is in a coordinate container I use for test type things. 8.0.10.

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/9/f9d50af96b59f98bca347d9d05e6e3a8eaa66b34.png)

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [September 22, 2020, 3:06pm UTC](https://forum.inductiveautomation.com/t/perspective-limit-text-input-field-to-regex-pattern/38936/10 "2020-09-22T15:06:13Z")

</div>

This is what I get:

 ![NumericEntryLimit](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/e/9e327234f0d51c2598ac326a8430ee77735fe841.gif)

Even after pressing enter it doesn’t update it

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [September 22, 2020, 3:57pm UTC](https://forum.inductiveautomation.com/t/perspective-limit-text-input-field-to-regex-pattern/38936/11 "2020-09-22T15:57:00Z")

</div>

And your onChange script is attached to `text`? I can’t see it in your video.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [September 22, 2020, 8:46pm UTC](https://forum.inductiveautomation.com/t/perspective-limit-text-input-field-to-regex-pattern/38936/12 "2020-09-22T20:46:23Z")

</div>

Yep. You can see that the “ddf” I entered don’t appear in the text in the props, so it must be working in the background, just doesn’t seem to update the display 🤔

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [September 22, 2020, 10:32pm UTC](https://forum.inductiveautomation.com/t/perspective-limit-text-input-field-to-regex-pattern/38936/13 "2020-09-22T22:32:05Z")

</div>

I think the problem is that you’re actively setting the text during a value change, and so the back-end schema is correct, but the front-end input is out-of-sync. I’ll look into it.

In the meantime, I found a hacky workaround:

1. Place a custom property: `stripped_text`.
2. Bind `TextField.custom.stripped_text` to `props.text`.
3. Place a change script on `TextField.custom.stripped_text` which performs your logic.  
4a. On the Text Field, you’ll need to use the `onKeyPress` Event to capture Key events for Enter, Tab, and Escape, and you’ll also need to use `onBlur` to capture the occasional click-out.  
4b. The `onKeyPress` and `onBlur` Events should use the following script.

```auto
    desired = self.custom.stripped_text
	self.props.text = ''
	self.props.text = desired

```

---

<div class="post-metadata">

**Author:** ![ynejati](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ynejati/32/6093_2.png) [@ynejati](https://forum.inductiveautomation.com/u/ynejati)\
**Post date:** [October 20, 2020, 5:00pm UTC](https://forum.inductiveautomation.com/t/perspective-limit-text-input-field-to-regex-pattern/38936/14 "2020-10-20T17:00:54Z")

</div>

The NEF does allow blanks. Just set the `format` to `none`.
