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. Password validation using regExp
Qt 6.11 is out! See what's new in the release blog

Password validation using regExp

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 3.4k 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.
  • D Offline
    D Offline
    dauuser
    wrote on last edited by
    #1

    hy @ all
    I'm trying to implement a password validation using regExps, but as you can guess, it's not working.

    The password must have the following properties:
    8-16 digits long
    must contain at least one lower case letter, one upper case letter and one digit.
    here is my code:

    TextInput{
         id: passwordInputBox;
         width: parent.width;
         echoMode: TextInput.Password;
         onTextChanged: { console.log(text) }
         validator: RexExpValidator { regExp: /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,16}$/ }
    }
    

    now if I try to enter my password in the textInput, nothing happens. I can't write a single letter in it.
    copy&paste of a correct password works, e g "aA139493a". but also just "aA1" works, despite the fact it's not 8 digits long. so after I copy three correct digits in my textInput, I am able to edit the rest of the password string up to 16 digits.
    I can't explain this behavior, really need some help here.

    thanks for any help.

    raven-worxR 1 Reply Last reply
    0
    • D dauuser

      hy @ all
      I'm trying to implement a password validation using regExps, but as you can guess, it's not working.

      The password must have the following properties:
      8-16 digits long
      must contain at least one lower case letter, one upper case letter and one digit.
      here is my code:

      TextInput{
           id: passwordInputBox;
           width: parent.width;
           echoMode: TextInput.Password;
           onTextChanged: { console.log(text) }
           validator: RexExpValidator { regExp: /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,16}$/ }
      }
      

      now if I try to enter my password in the textInput, nothing happens. I can't write a single letter in it.
      copy&paste of a correct password works, e g "aA139493a". but also just "aA1" works, despite the fact it's not 8 digits long. so after I copy three correct digits in my textInput, I am able to edit the rest of the password string up to 16 digits.
      I can't explain this behavior, really need some help here.

      thanks for any help.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @dauuser
      Such a complex restriction wont be achievable with a regex

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • D Offline
        D Offline
        dauuser
        wrote on last edited by
        #3

        uh okay. so I have to do this by using js, good to know. thank you!

        J 1 Reply Last reply
        1
        • D dauuser

          uh okay. so I have to do this by using js, good to know. thank you!

          J Offline
          J Offline
          JosephMills
          wrote on last edited by JosephMills
          #4

          @dauuser Here is something that I use to do just that I watch the TextInput and when the textChanges I hit this slot. In turn this handles the length and all that but also gives back a value of 0 --100 as to how strong it is. Could use some work but it works. full source code below.

          void PasswordStrength::handelUpdate(const QString upString)
          {
              if(upString.length() < 1 ){
                  emit error(NotEnoughtCharacters);
                  return;
              }
             
             // Set this to your liking 
              const double lengthFactor = m_reasonablePasswordLength / 8.0;
              int pwlength = (int) ( upString.length()/ lengthFactor);
              if (pwlength > 5) {
                  pwlength = 5;
              }
          
              const QRegExp numRxp("[0-9]", Qt::CaseSensitive, QRegExp::RegExp);
              int numeric = (int) (upString.count(numRxp) / lengthFactor);
              if (numeric > 3) {
                  numeric = 3;
              }
          
              const QRegExp symbRxp("\\W", Qt::CaseInsensitive, QRegExp::RegExp);
              int numsymbols = (int) (upString.count(symbRxp) / lengthFactor);
              if (numsymbols > 3) {
                  numsymbols = 3;
              }
          
              const QRegExp upperRxp("[A-Z]", Qt::CaseSensitive, QRegExp::RegExp);
              int upper = (int) (upString.count(upperRxp) / lengthFactor);
              if (upper > 3) {
                  upper = 3;
              }
          
              int pwstrength=((pwlength*10)-20) + (numeric*10) + (numsymbols*15) + (upper*10);
          
          
          //    qDebug() << pwstrength;
          
              if ( pwstrength == 0 ) {
                  setPwdStrength(0);
                  setStrenghtString(pwstrength);
          
                  return;
              }
          
              if ( pwstrength > 100 ) {
                  setPwdStrength(100);
                  setStrenghtString(pwstrength);
          
                  return;
              }
          
              if (pwstrength > 1 && pwstrength < 99){
              setPwdStrength(pwstrength);
              setStrenghtString(pwstrength);
          
              }
          }
          
          

          sources file

          header

          1 Reply Last reply
          1
          • D Offline
            D Offline
            dauuser
            wrote on last edited by dauuser
            #5

            @JosephMills thank you for sharing your code, but I found a faster solution for my project:

            I just created a js function in my TextInput, which changes for testing the color of my box. Works fine.

            function validation(text){
                            var regExp = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,16}$/;
                            if(regExp.test(text)) passwordInputBox.color = "#62aa46";
                            else passwordInputBox.color = "red";
                        }
            
                        onTextChanged: {
                            validation(text);
                        }
            
            1 Reply Last reply
            1

            • Login

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