Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Restrict user input in QLineEdit using Regex
Forum Updated to NodeBB v4.3 + New Features

Restrict user input in QLineEdit using Regex

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 2.1k 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.
  • A Offline
    A Offline
    Anthony Steer
    wrote on 16 Jun 2021, 18:25 last edited by
    #1

    I am trying to limit the user input in a QLineEdit box.
    The three requirements are:

    • String must start with a letter (upper or lower case)
    • list itemThe following characters can be any combination of letters and numbers
    • list itemTotal string length must be between 1 and 12 characters

    This is the regex I have come up with (and verified on Regex101.com)

    ^(?=.{1,12}$)^[]a-zA-Z][a-zA-Z0-9]*$
    

    When I use this with the QLIneEdit, My length is restriced to 12 chars, but I can enter illegal characters.
    If I remove the length check, I can orrectly restricted to a first letter followed by alpha-numerics but, obviously, my length is not restricted.

    How do I achieve both aspects of this regex for a QLineEdit widget?

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 16 Jun 2021, 18:39 last edited by
      #2

      You are overcomplicating it. This satisfies the requirements: [a-zA-Z][a-zA-Z0-9]{0,11}

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      3
      • A Offline
        A Offline
        Anthony Steer
        wrote on 16 Jun 2021, 18:53 last edited by Anthony Steer
        #3

        @VRonin said in Restrict user input in QLineEdit using Regex:

        [a-zA-Z][a-zA-Z0-9]{0,11}

        Ha ha - brilliant - thank you :)

        Any idea why mine did not behave as expected despite it working on regex tester? just curious now. Thanks

        V 1 Reply Last reply 16 Jun 2021, 18:59
        0
        • A Anthony Steer
          16 Jun 2021, 18:53

          @VRonin said in Restrict user input in QLineEdit using Regex:

          [a-zA-Z][a-zA-Z0-9]{0,11}

          Ha ha - brilliant - thank you :)

          Any idea why mine did not behave as expected despite it working on regex tester? just curious now. Thanks

          V Offline
          V Offline
          VRonin
          wrote on 16 Jun 2021, 18:59 last edited by
          #4

          @Anthony-Steer said in Restrict user input in QLineEdit using Regex:

          just curious now

          Can you post your code?

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          A 1 Reply Last reply 16 Jun 2021, 19:11
          0
          • V VRonin
            16 Jun 2021, 18:59

            @Anthony-Steer said in Restrict user input in QLineEdit using Regex:

            just curious now

            Can you post your code?

            A Offline
            A Offline
            Anthony Steer
            wrote on 16 Jun 2021, 19:11 last edited by
            #5

            @VRonin

                m_Validator.setRegularExpression( QRegularExpression(  "^(?=.{1,12}$)^[a-zA-Z][a-zA-Z0-9]*$" ) );
                ui.lineEdit->setValidator( &m_Validator );
            
            // where lineEdit is a QLineEdit widget
            // and m_Validator is QRegularExpressionValidator
            
            1 Reply Last reply
            0
            • V Offline
              V Offline
              VRonin
              wrote on 16 Jun 2021, 19:35 last edited by
              #6

              Works for me...

              QLineEdit* lineEdit = new QLineEdit(this);
                      lineEdit->setValidator(new QRegularExpressionValidator(QRegularExpression("(?=.{1,12}$)^[a-zA-Z][a-zA-Z0-9]*"),this));
              

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              A 1 Reply Last reply 16 Jun 2021, 20:22
              0
              • V VRonin
                16 Jun 2021, 19:35

                Works for me...

                QLineEdit* lineEdit = new QLineEdit(this);
                        lineEdit->setValidator(new QRegularExpressionValidator(QRegularExpression("(?=.{1,12}$)^[a-zA-Z][a-zA-Z0-9]*"),this));
                
                A Offline
                A Offline
                Anthony Steer
                wrote on 16 Jun 2021, 20:22 last edited by
                #7

                @VRonin
                That is bizarre, apart from the fact that my widget is created in the designer, my code is now identical to yours and I find that I can enter numbers as the first character. I don;t know if it makes a difference but I am using Qt5.13.0.

                But anyway, thanks for your help.

                Best Regards

                Tony

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  ChrisW67
                  wrote on 17 Jun 2021, 06:03 last edited by
                  #8

                  You will be able to enter anything you like because anything you start entering might eventually become acceptable because you edit it. If you try to finish editing (exit the widget) while the content is unacceptable, QLineEdit::acceptableInput() will return false. As a result, the line edit's returnPressed() and editingFinished() signals will not be emitted.

                  The input mask can stop invalid input from the outset, but is a bit limited. You can subclass QLineEdit and intercept keypresses to implement a smarter filter.

                  1 Reply Last reply
                  1

                  1/8

                  16 Jun 2021, 18:25

                  • Login

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