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. [SOLVED] How can a lineedit accept only (ASCII alphanumeric character required. A-Z, a-z, 0-9.)
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] How can a lineedit accept only (ASCII alphanumeric character required. A-Z, a-z, 0-9.)

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 17.3k Views 1 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.
  • L Offline
    L Offline
    Leon
    wrote on last edited by
    #1

    I am seeing "this":http://doc.qt.nokia.com/latest/qlineedit.html#inputMask-prop but i can't understant what to put at the inputmask at qtdesigner..

    !http://img689.imageshack.us/img689/5366/screenshot1oa.png(screen)!

    This isn't correct. So how to accept only A-Z,a-z and 0-9?

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

      to solve your problem, you may use the "QRegExpValidator":http://doc.qt.nokia.com/4.7/qregexpvalidator.html
      @
      lineEdit->setValidator(new QRegExpValidator( QRegExp("\w"), this ));
      @

      I have used recently the QRegExpValidator instead of inputMask for very same reason. Please verify the validity of the RegExp I defined.

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

      1 Reply Last reply
      0
      • O Offline
        O Offline
        octal
        wrote on last edited by
        #3

        \w is equivalent to [A-Za-z0-9_]. For alphanumeric characters, you may want to use [A-Za-z0-9]

        1 Reply Last reply
        0
        • L Offline
          L Offline
          Leon
          wrote on last edited by
          #4

          Thanks for the answers. But with

          @ ui->lineEdit->setValidator(new QRegExpValidator( QRegExp("[A-Za-z0-9_]"), this ));
          @

          i cant write more than one letter/number at the lineedit when i run the program.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            Leon
            wrote on last edited by
            #5

            Hmm as seen "here":http://developer.qt.nokia.com/forums/viewthread/8432 with this it works fine.

            @ ui->lineEdit->setValidator(new QRegExpValidator( QRegExp("[A-Za-z0-9_]{0,255}"), this ));
            @

            So adding {0,255} let me put more than i letter. But why? is this correct like this?

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dangelog
              wrote on last edited by
              #6

              Because that's how regexps work? The class "[A-za-z0-9]" matches one character belonging to that class. To match more you have to use a quantifier (f.i. * for an unbounded number of repetitions).

              Software Engineer
              KDAB (UK) Ltd., a KDAB Group company

              1 Reply Last reply
              0
              • O Offline
                O Offline
                octal
                wrote on last edited by
                #7

                Well, I'm not a regexp specialist but that's how regexp work. [A-Za-z0-9_] only matches one character.

                If you want to match an undefined amount of characters, you'll have to use one of these metacharacteres :

                • '*' Matches zero or more times ;
                • '+' Matches one or more times.

                For example :

                @ui->lineEdit->setValidator(new QRegExpValidator( QRegExp("[A-Za-z0-9_]+"), this ));@

                If I'm not wrong, you can also use :

                @[A-Za-z0-9_]{1,}@

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  Leon
                  wrote on last edited by
                  #8

                  Ok thank you guys! :)

                  1 Reply Last reply
                  0

                  • Login

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