How to pass a variable to a regular expression validator in a TextInput? QML
-
wrote on 16 Jul 2024, 00:33 last edited by dheerendra
Posted this in QML side of the forums since I thought that'd be more appropriate
So suppose we have a TextInput with a regular expression validator
TextInput { id: hexNumber validator: RegularExpressionValidator { regularExpression: /[0-9A-F]+/ } }
https://doc.qt.io/qt-6/qml-qtquick-regularexpressionvalidator.html
How might we pass a variable containing "[0-9A-F]+" to it instead of passing the value to it directly?
The following code (what I tried) doesn't work
property string exampleValidationString: "[0-9A-F]+" var regExpVariable= new RegExp(exampleValidationString); TextInput{ /* User input validation */ validator: RegularExpressionValidator { regularExpression: regExpVariable } }
I also tried passing a property string to it and it didn't like that
-
Following should work. Check this.
```
property string exampleValidationString: "[0-9A-F]+";
property var regExpVariable : new RegExp(exampleValidationString)TextInput{ anchors.fill: parent //validator:RegularExpressionValidator{ regularExpression:RegExp(exampleValidationString)} validator:RegularExpressionValidator{ regularExpression:regExpVariable} }
-
-
Following should work. Check this.
```
property string exampleValidationString: "[0-9A-F]+";
property var regExpVariable : new RegExp(exampleValidationString)TextInput{ anchors.fill: parent //validator:RegularExpressionValidator{ regularExpression:RegExp(exampleValidationString)} validator:RegularExpressionValidator{ regularExpression:regExpVariable} }
wrote on 16 Jul 2024, 07:47 last edited by@dheerendra Thanks for the help! As far as I can tell, that works
1/3