Hello, I have this code and I am trying to add a regular expression to my component but did not works:
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
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…
I think , the code has the format, so you can read now better
You’ll need to use python’s re
module. Strings don’t have a validationPattern
method.
I imported, but now I just let :
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
Also, (but I am not a Python expert), are you searching the correct variable?
Esme_Lopez:
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
.
Yes, but textfield
is also a string. It doesn’t have a validationPattern
property or method.
Something like this:
import re
......
if re.match(r'[A-Za-z]{2,256}', textfield):
do something
https://docs.python.org/2.7/library/re.html
1 Like
Why not use the formatted text field?
3 Likes
Because in this version 8.1 I don’t have these options
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)
1 Like
O but I see why, I was using the normal text field component, thanks
But I also have differents strings in the dropdown list, that is why I was trying to do it by script
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.
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.
I don’t understand what you are saying/asking.
He wants the dropdown list to determine the regex pattern I think
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.
if event.propertyName == 'selectedLabel':
regex = event.source.selectedStringValue
event.source.parent.getComponent('Formatted Text Field').validationPattern = regex
And If I want validate by script a Formatted Text Field like :
If textfield == 'regular expresion'
Send message
The equals operator doesn't do regular expressions. You must use the re
module and its .match()
method. Like the example I gave.