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. TextField with RegExpValidator
Qt 6.11 is out! See what's new in the release blog

TextField with RegExpValidator

Scheduled Pinned Locked Moved Solved QML and Qt Quick
9 Posts 4 Posters 4.2k Views 4 Watching
  • 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.
  • ekkescornerE Offline
    ekkescornerE Offline
    ekkescorner
    Qt Champions 2016
    wrote on last edited by
    #1

    I want to set a validator which allows following input into a (QtQuickControls2) TextField:

    nnn/nnn
    where nnn can have 2 or 3 digits and a minimum of 30 and a maximum of 300
    per ex

    80/120
    110/90

    RegEx is very powerful, but I'm always fighting with the syntax ;-)

    can someone help with the exact syntax ?

    validator: RegExpValidator{regExp: "???"}
    

    thx

    ekke ... Qt Champion 2016 | 2024 ... mobile business apps
    5.15 --> 6.9 https://t1p.de/ekkeChecklist
    QMake --> CMake https://t1p.de/ekkeCMakeMobileApps

    K 1 Reply Last reply
    0
    • ekkescornerE ekkescorner

      I want to set a validator which allows following input into a (QtQuickControls2) TextField:

      nnn/nnn
      where nnn can have 2 or 3 digits and a minimum of 30 and a maximum of 300
      per ex

      80/120
      110/90

      RegEx is very powerful, but I'm always fighting with the syntax ;-)

      can someone help with the exact syntax ?

      validator: RegExpValidator{regExp: "???"}
      

      thx

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @ekkescorner

      Do you mean the regExp syntax?

      Derived from c++ probably more "\\d{2,3}/\\d{2,3}".
      This is tested with the regExp example in C++. Since I do not know Qt Quick, I do not know the requirements for backslashes and their duplication.

      Note: this satisfies also "80/90" and "100/120". Possibly this is already enough for you.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • K Offline
        K Offline
        koahnig
        wrote on last edited by
        #3

        Playing a bit more with the example
        it would be: "^(\\d{2,2}/\\d{3,3})|(\\d{3,3}/\\d{2,2})$" and allows only digits and forward as in your description.

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply
        1
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          While @koahnig is correct from a C++ POV, JavaScript syntax is a bit different and IIRC RegExpValidator takes a JS regex.
          Something like /\d{2,3}\/\d{2,3}/ should be enough.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          ekkescornerE 2 Replies Last reply
          1
          • SGaistS SGaist

            Hi,

            While @koahnig is correct from a C++ POV, JavaScript syntax is a bit different and IIRC RegExpValidator takes a JS regex.
            Something like /\d{2,3}\/\d{2,3}/ should be enough.

            ekkescornerE Offline
            ekkescornerE Offline
            ekkescorner
            Qt Champions 2016
            wrote on last edited by
            #5

            @SGaist said in TextField with RegExpValidator:

            Hi,

            While @koahnig is correct from a C++ POV, JavaScript syntax is a bit different and IIRC RegExpValidator takes a JS regex.
            Something like /\d{2,3}\/\d{2,3}/ should be enough.

            so to check min and max I can split the text by '/' and check both numbers

            thx for helping - will try it out

            ekke ... Qt Champion 2016 | 2024 ... mobile business apps
            5.15 --> 6.9 https://t1p.de/ekkeChecklist
            QMake --> CMake https://t1p.de/ekkeCMakeMobileApps

            1 Reply Last reply
            0
            • SGaistS SGaist

              Hi,

              While @koahnig is correct from a C++ POV, JavaScript syntax is a bit different and IIRC RegExpValidator takes a JS regex.
              Something like /\d{2,3}\/\d{2,3}/ should be enough.

              ekkescornerE Offline
              ekkescornerE Offline
              ekkescorner
              Qt Champions 2016
              wrote on last edited by
              #6

              @SGaist said in TextField with RegExpValidator:

              Hi,

              While @koahnig is correct from a C++ POV, JavaScript syntax is a bit different and IIRC RegExpValidator takes a JS regex.
              Something like /\d{2,3}\/\d{2,3}/ should be enough.

              did this:

              validator: RegExpValidator{regExp: "/\d{2,3}\/\d{2,3}/"}
              

              but got an error:

              Invalid property assignment: regular expression expected; use /pattern/ syntax
              

              regexp mentioned @koahnig also don't work

              ekke ... Qt Champion 2016 | 2024 ... mobile business apps
              5.15 --> 6.9 https://t1p.de/ekkeChecklist
              QMake --> CMake https://t1p.de/ekkeCMakeMobileApps

              1 Reply Last reply
              0
              • p3c0P Offline
                p3c0P Offline
                p3c0
                Moderators
                wrote on last edited by
                #7

                @ekkescorner Try without quotes.

                157

                ekkescornerE 1 Reply Last reply
                1
                • p3c0P p3c0

                  @ekkescorner Try without quotes.

                  ekkescornerE Offline
                  ekkescornerE Offline
                  ekkescorner
                  Qt Champions 2016
                  wrote on last edited by
                  #8

                  @p3c0 thx it works without the quotes

                  next question:
                  if I want to allow
                  one character of

                  /
                  .
                  ,
                  

                  how must the regexp be constructed ?

                  reason:
                  on Android and iOS want to use virtual numeric keyboard where I don't have a '/'
                  but I can enter . on Android and , on iOS (german locale)
                  so I do a text.replace changing . or , into /
                  but using the RegExp only allowing a / as separator I cannot enter . or ,

                  thx again

                  ekke ... Qt Champion 2016 | 2024 ... mobile business apps
                  5.15 --> 6.9 https://t1p.de/ekkeChecklist
                  QMake --> CMake https://t1p.de/ekkeCMakeMobileApps

                  ekkescornerE 1 Reply Last reply
                  0
                  • ekkescornerE ekkescorner

                    @p3c0 thx it works without the quotes

                    next question:
                    if I want to allow
                    one character of

                    /
                    .
                    ,
                    

                    how must the regexp be constructed ?

                    reason:
                    on Android and iOS want to use virtual numeric keyboard where I don't have a '/'
                    but I can enter . on Android and , on iOS (german locale)
                    so I do a text.replace changing . or , into /
                    but using the RegExp only allowing a / as separator I cannot enter . or ,

                    thx again

                    ekkescornerE Offline
                    ekkescornerE Offline
                    ekkescorner
                    Qt Champions 2016
                    wrote on last edited by
                    #9

                    @ekkescorner found it:

                    /\d{2,3}[\.\,\/]\d{2,3}/
                    

                    thx all for help

                    ekke ... Qt Champion 2016 | 2024 ... mobile business apps
                    5.15 --> 6.9 https://t1p.de/ekkeChecklist
                    QMake --> CMake https://t1p.de/ekkeCMakeMobileApps

                    1 Reply Last reply
                    3

                    • Login

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