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. Using a validator for a TextField
Forum Updated to NodeBB v4.3 + New Features

Using a validator for a TextField

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
7 Posts 2 Posters 1.0k Views 2 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.
  • tomyT Offline
    tomyT Offline
    tomy
    wrote on last edited by tomy
    #1

    Hi all,

    In this simple example, I want when the user types a text from 3 to 10 alphabet characters, then the button below that is enabled. But this code below doesn't work and I also get this error:

    qrc:/main.qml:20:41: Unable to assign QString to QRegExp

    How to solve this please and have the project work as described above?

    import QtQuick 2.12
    import QtQuick.Window 2.2
    import QtQuick.Controls 2.12
    
    Window {
        visible: true
        width: 640; height: 480
    
        Rectangle {
            width: parent.width / 2; height: parent.height / 2
            anchors.centerIn: parent
            color: "linen"
    
            TextField {
                id: textFiled
                font.pixelSize: 15
                anchors.centerIn: parent
    
                placeholderText: qsTr("Enter the name ...")
                validator:RegExpValidator { regExp: ("[A-Za-z]{3, 10}") }
    
                onAccepted: myButton.enabled = true
            }
    
            Button {
                id: myButton
                enabled: false
                anchors.top: textFiled.bottom
                anchors.topMargin: 15
                anchors.horizontalCenter: textFiled.horizontalCenter
    
                onClicked: console.log("Button clicked")
            }
        }
    }
    
    1 Reply Last reply
    0
    • tomyT Offline
      tomyT Offline
      tomy
      wrote on last edited by
      #2

      I used this:

      validator: RegExpValidator {
                    regExp: /[A-Za-z]{1,20}/
                  }
      

      And it worked.
      But how to modify it to be able to add space characters too?

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

        Hi,

        If it's only the space bar space char that you want, you can simply add it as last char to your character range.

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

        tomyT 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          If it's only the space bar space char that you want, you can simply add it as last char to your character range.

          tomyT Offline
          tomyT Offline
          tomy
          wrote on last edited by tomy
          #4

          @SGaist

          Yes, but how to make a character valid for entering texts?
          I mean in code. For example how to add that space character to the range?

          This way!?

          regExp: /[A-Za-z-" "]{1,20}/
          
          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Just the space, nothing more: /[A-Za-z ]{1,20}/

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

            tomyT 1 Reply Last reply
            4
            • SGaistS SGaist

              Just the space, nothing more: /[A-Za-z ]{1,20}/

              tomyT Offline
              tomyT Offline
              tomy
              wrote on last edited by
              #6

              @SGaist
              Thank you. It worked both ways.

              One other question, when I type the text and it's valid, it won't be accepted and consequently the button won't be enabled either, unless I press Enter. How to make it enabled whenever a valid text is typed in the text field, without a need for pressing Enter, please?

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

                In that case wouldn't the editingFinished be what you are looking for ?

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

                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