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. Regular Expression for date and time(DD/MM/YYYY hh:mm:ss) in QML
QtWS25 Last Chance

Regular Expression for date and time(DD/MM/YYYY hh:mm:ss) in QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmlqtquick2regex
5 Posts 3 Posters 4.6k 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.
  • P Offline
    P Offline
    pra7
    wrote on last edited by
    #1

    In QML2 I didn't find any Calender control and I have implemented a control which takes date and time as input and I am using the regular expression for the validation which matches dates including leap year and other validations.

    The main problem is space/backspace should also be considered as a valid for example:

    \s\s/\s\s/\s\s \s\s:\s\s:\s\s

    Following is the code :

    TextField{
        id:textEditDate
    
        width:parent.width * 0.50
        height:parent.height
        text : "01/01/2017 00:00:00"
    
        inputMask: "99/99/9999 99:99:99"
    
        validator: RegExpValidator { regExp: /^(((([0\s][1-9\s]|[1\s][0-9\s]|[2\s][0-8\s])[\/]([0\s][1-9\s]|[1\s][012\s]))|((29|30|31)[\/]([0\s][13578\s]|[1\s][02\s]))|((29|30)[\/]([0\s][4,6,9]|11)))[\/]([19\s[2-9\s][0-9\s])\d\d|(^29[\/]02[\/]([19\s]|[2-9\s][0-9\s])(00|04|08|12|16|20|24|28|32|36|40|44|48|52|56|60|64|68|72|76|80|84|88|92|96)))\s([0-1\s]?[0-9\s]|2[0-3\s]):([0-5\s][0-9\s]):([0-5\s][0-9\s])$/}
    
        horizontalAlignment: Text.AlignHCenter
        inputMethodHints: Qt.ImhDigitsOnly
    }
    

    Now, everything works well except for the year and I am not able to match backspace/space for the year and user is not able to clear the year.

    Can you please suggest how to achieve this ? or is there any other method to do this.

    JonBJ 1 Reply Last reply
    0
    • P pra7

      In QML2 I didn't find any Calender control and I have implemented a control which takes date and time as input and I am using the regular expression for the validation which matches dates including leap year and other validations.

      The main problem is space/backspace should also be considered as a valid for example:

      \s\s/\s\s/\s\s \s\s:\s\s:\s\s

      Following is the code :

      TextField{
          id:textEditDate
      
          width:parent.width * 0.50
          height:parent.height
          text : "01/01/2017 00:00:00"
      
          inputMask: "99/99/9999 99:99:99"
      
          validator: RegExpValidator { regExp: /^(((([0\s][1-9\s]|[1\s][0-9\s]|[2\s][0-8\s])[\/]([0\s][1-9\s]|[1\s][012\s]))|((29|30|31)[\/]([0\s][13578\s]|[1\s][02\s]))|((29|30)[\/]([0\s][4,6,9]|11)))[\/]([19\s[2-9\s][0-9\s])\d\d|(^29[\/]02[\/]([19\s]|[2-9\s][0-9\s])(00|04|08|12|16|20|24|28|32|36|40|44|48|52|56|60|64|68|72|76|80|84|88|92|96)))\s([0-1\s]?[0-9\s]|2[0-3\s]):([0-5\s][0-9\s]):([0-5\s][0-9\s])$/}
      
          horizontalAlignment: Text.AlignHCenter
          inputMethodHints: Qt.ImhDigitsOnly
      }
      

      Now, everything works well except for the year and I am not able to match backspace/space for the year and user is not able to clear the year.

      Can you please suggest how to achieve this ? or is there any other method to do this.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @pra7 I'm afraid this does not help with your problem, but attempting to deal with validating leap year dates via regular expressions seems extraordinary to me!

      1 Reply Last reply
      1
      • JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        For your Calendar control:

        • https://stackoverflow.com/questions/21153199/is-there-any-datepicker-control-for-qt-5
        • http://doc.qt.io/qt-5/qml-qtquick-controls-calendar.html

        ?

        But I don't imagine there will be a date+time control. I had to implement that myself in previous (non-Qt) projects.

        P 1 Reply Last reply
        0
        • JonBJ JonB

          For your Calendar control:

          • https://stackoverflow.com/questions/21153199/is-there-any-datepicker-control-for-qt-5
          • http://doc.qt.io/qt-5/qml-qtquick-controls-calendar.html

          ?

          But I don't imagine there will be a date+time control. I had to implement that myself in previous (non-Qt) projects.

          P Offline
          P Offline
          pra7
          wrote on last edited by
          #4

          @JNBarchan Thanks for the links. I need calendar control in QML2, not in QML1.

          E 1 Reply Last reply
          0
          • P pra7

            @JNBarchan Thanks for the links. I need calendar control in QML2, not in QML1.

            E Offline
            E Offline
            Eeli K
            wrote on last edited by
            #5

            @pra7 See also https://forum.qt.io/topic/82700/datepicker-anyone. BTW you must mean Quick Controls 2, not QML2. You also can use Controls 1 elements in Controls 2 application, although the look&feel may be a problem.

            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