Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to pass a variable to a regular expression validator in a TextInput? QML
Forum Updated to NodeBB v4.3 + New Features

How to pass a variable to a regular expression validator in a TextInput? QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 329 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on 16 Jul 2024, 00:33 last edited by dheerendra
    #1

    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

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 16 Jul 2024, 04:11 last edited by dheerendra
      #2

      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}
          }
      

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      ? 1 Reply Last reply 16 Jul 2024, 07:47
      0
      • System has marked this topic as solved on 16 Jul 2024, 07:41
      • D dheerendra
        16 Jul 2024, 04:11

        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}
            }
        
        ? Offline
        ? Offline
        A Former User
        wrote on 16 Jul 2024, 07:47 last edited by
        #3

        @dheerendra Thanks for the help! As far as I can tell, that works

        1 Reply Last reply
        0

        1/3

        16 Jul 2024, 00:33

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved