# Text field validation

**URL:** <https://forum.inductiveautomation.com/t/text-field-validation/57028>\
**Category:** Ignition\
**Tags:** ignition81, scripting, vision\
**Created:** [March 9, 2022, 7:16pm UTC](https://forum.inductiveautomation.com/t/text-field-validation/57028 "2022-03-09T19:16:10Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Esme\_Lopez](https://avatars.discourse-cdn.com/v4/letter/e/ecb155/32.png) [@Esme\_Lopez](https://forum.inductiveautomation.com/u/Esme_Lopez)\
**Post date:** [March 9, 2022, 7:16pm UTC](https://forum.inductiveautomation.com/t/text-field-validation/57028/1 "2022-03-09T19:16:10Z")

</div>

Hello, I have this code and I am trying to add a regular expression to my component but did not works:

```auto
textfield = event.source.parent.getComponent('Text Field').text
search = event.source.parent.getComponent ('dropdown list').selectedStringValue

if search=='Number':
       if search != search.validationPattern("[a-zA-Z]{2,256}"):
             system.gui.messageBox('Incorrect , only numbers')
      else:
            do something

thanks

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 9, 2022, 7:21pm UTC](https://forum.inductiveautomation.com/t/text-field-validation/57028/4 "2022-03-09T19:21:10Z")

</div>

Paste your code, then select it again so the code (and only the code) is completely highlighted. Then click the pre-formatted text button. “thanks” would be **outside** the code block…

---

<div class="post-metadata">

**Author:** ![Esme\_Lopez](https://avatars.discourse-cdn.com/v4/letter/e/ecb155/32.png) [@Esme\_Lopez](https://forum.inductiveautomation.com/u/Esme_Lopez)\
**Post date:** [March 9, 2022, 7:22pm UTC](https://forum.inductiveautomation.com/t/text-field-validation/57028/5 "2022-03-09T19:22:25Z")

</div>

I think , the code has the format, so you can read now better

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 9, 2022, 7:24pm UTC](https://forum.inductiveautomation.com/t/text-field-validation/57028/6 "2022-03-09T19:24:17Z")

</div>

You’ll need to use python’s `re` module. Strings don’t have a `validationPattern` method.

---

<div class="post-metadata">

**Author:** ![Esme\_Lopez](https://avatars.discourse-cdn.com/v4/letter/e/ecb155/32.png) [@Esme\_Lopez](https://forum.inductiveautomation.com/u/Esme_Lopez)\
**Post date:** [March 9, 2022, 7:28pm UTC](https://forum.inductiveautomation.com/t/text-field-validation/57028/7 "2022-03-09T19:28:11Z")

</div>

I imported, but now I just let :

```auto
textfield = event.source.parent.getComponent('Text Field').text
search = event.source.parent.getComponent ('dropdown list').selectedStringValue

if search=='Number':
       if search.validationPattern != "[a-zA-Z]{2,256}":
             system.gui.messageBox('Incorrect , only numbers')
      else:
            do something

```

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [March 9, 2022, 7:28pm UTC](https://forum.inductiveautomation.com/t/text-field-validation/57028/8 "2022-03-09T19:28:52Z")

</div>

Also, (but I am not a Python expert), are you searching the correct variable?

> [@Esme\_Lopez](#):
>
> ```auto
> textfield = event.source.parent.getComponent('Text Field').text
> search = event.source.parent.getComponent ('dropdown list').selectedStringValue
> 
> if search ==' Number':
> if textfield != textfield.validationPattern("[a-zA-Z]{2,256}"):
> system.gui.messageBox('Incorrect , only numbers')
> else:
> do something
> 
> ```

You are testing `search`. I think maybe you should be testing `textfield`.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 9, 2022, 7:33pm UTC](https://forum.inductiveautomation.com/t/text-field-validation/57028/9 "2022-03-09T19:33:04Z")

</div>

Yes, but `textfield` is also a string. It doesn’t have a `validationPattern` property or method.  
Something like this:

```auto
import re

......

if re.match(r'[A-Za-z]{2,256}', textfield):
    do something

```

[https://docs.python.org/2.7/library/re.html](https://docs.python.org/2.7/library/re.html)

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [March 9, 2022, 7:58pm UTC](https://forum.inductiveautomation.com/t/text-field-validation/57028/10 "2022-03-09T19:58:15Z")

</div>

Why not use the formatted text field?  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/2/12d3ff64c5a18de7d448f3f561efc3560ce71bad.png)

---

<div class="post-metadata">

**Author:** ![Esme\_Lopez](https://avatars.discourse-cdn.com/v4/letter/e/ecb155/32.png) [@Esme\_Lopez](https://forum.inductiveautomation.com/u/Esme_Lopez)\
**Post date:** [March 9, 2022, 8:32pm UTC](https://forum.inductiveautomation.com/t/text-field-validation/57028/11 "2022-03-09T20:32:03Z")

</div>

Because in this version 8.1 I don’t have these options

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [March 9, 2022, 8:33pm UTC](https://forum.inductiveautomation.com/t/text-field-validation/57028/12 "2022-03-09T20:33:48Z")

</div>

Huh? I’m using v8.1.14 right now, and I’ve been using this since v7.9.

EDIT:  
There is a whole different component called the `formatted text field`  
[Vision - Formatted Text Field - Ignition User Manual 8.1 - Ignition Documentation (inductiveautomation.com)](https://docs.inductiveautomation.com/display/DOC81/Vision+-+Formatted+Text+Field)

---

<div class="post-metadata">

**Author:** ![Esme\_Lopez](https://avatars.discourse-cdn.com/v4/letter/e/ecb155/32.png) [@Esme\_Lopez](https://forum.inductiveautomation.com/u/Esme_Lopez)\
**Post date:** [March 9, 2022, 8:46pm UTC](https://forum.inductiveautomation.com/t/text-field-validation/57028/13 "2022-03-09T20:46:09Z")

</div>

O but I see why, I was using the normal text field component, thanks

---

<div class="post-metadata">

**Author:** ![Esme\_Lopez](https://avatars.discourse-cdn.com/v4/letter/e/ecb155/32.png) [@Esme\_Lopez](https://forum.inductiveautomation.com/u/Esme_Lopez)\
**Post date:** [March 9, 2022, 9:00pm UTC](https://forum.inductiveautomation.com/t/text-field-validation/57028/14 "2022-03-09T21:00:58Z")

</div>

But I also have differents strings in the dropdown list, that is why I was trying to do it by script

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [March 9, 2022, 9:35pm UTC](https://forum.inductiveautomation.com/t/text-field-validation/57028/15 "2022-03-09T21:35:53Z")

</div>

I suspect that you are creating a strange or bad user interface. Sit back, have a look at professionally designed applications, how the work flows, how the data is stored and start again.

---

<div class="post-metadata">

**Author:** ![Esme\_Lopez](https://avatars.discourse-cdn.com/v4/letter/e/ecb155/32.png) [@Esme\_Lopez](https://forum.inductiveautomation.com/u/Esme_Lopez)\
**Post date:** [March 10, 2022, 6:42pm UTC](https://forum.inductiveautomation.com/t/text-field-validation/57028/16 "2022-03-10T18:42:09Z")

</div>

It works, but I want to sent a message that only letters or numbers, but I write the regular expression from the properties of the component.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [March 10, 2022, 8:08pm UTC](https://forum.inductiveautomation.com/t/text-field-validation/57028/17 "2022-03-10T20:08:20Z")

</div>

I don’t understand what you are saying/asking.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [March 10, 2022, 8:18pm UTC](https://forum.inductiveautomation.com/t/text-field-validation/57028/18 "2022-03-10T20:18:03Z")

</div>

He wants the dropdown list to determine the regex pattern I think

---

<div class="post-metadata">

**Author:** ![Esme\_Lopez](https://avatars.discourse-cdn.com/v4/letter/e/ecb155/32.png) [@Esme\_Lopez](https://forum.inductiveautomation.com/u/Esme_Lopez)\
**Post date:** [March 10, 2022, 8:30pm UTC](https://forum.inductiveautomation.com/t/text-field-validation/57028/19 "2022-03-10T20:30:56Z")

</div>

Yes, that

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [March 10, 2022, 8:44pm UTC](https://forum.inductiveautomation.com/t/text-field-validation/57028/20 "2022-03-10T20:44:22Z")

</div>

That should be easy. Set your dropdown labels, like “Numbers only”, then set the string value for that label as the regex. In a property change event, set the formatted text field’s validation pattern according to the selected drop down item.

```auto
if event.propertyName == 'selectedLabel':
    regex = event.source.selectedStringValue
    event.source.parent.getComponent('Formatted Text Field').validationPattern = regex

```

---

<div class="post-metadata">

**Author:** ![Esme\_Lopez](https://avatars.discourse-cdn.com/v4/letter/e/ecb155/32.png) [@Esme\_Lopez](https://forum.inductiveautomation.com/u/Esme_Lopez)\
**Post date:** [March 17, 2022, 11:40pm UTC](https://forum.inductiveautomation.com/t/text-field-validation/57028/21 "2022-03-17T23:40:10Z")

</div>

And If I want validate by script a Formatted Text Field like :

```auto

If textfield == 'regular expresion' 
    Send message

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 18, 2022, 11:14am UTC](https://forum.inductiveautomation.com/t/text-field-validation/57028/22 "2022-03-18T11:14:49Z")

</div>

> [@Esme\_Lopez](#):
>
> `textfield == 'regular expresion'`

The equals operator doesn't do regular expressions. You must use the `re` module and its `.match()` method. Like the example I gave.
