Password validation using regExp



  • 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.


  • Moderators

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



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



  • @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



  • @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);
                }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.