Regex for checking that at least 3 of 4 different character groups exist

后端 未结 6 641
一个人的身影
一个人的身影 2020-12-01 13:04

I\'m trying to write a password validator.

How can I see if my supplied string contains at least 3 different character groups?

It\'s easy enough to check if

相关标签:
6条回答
  • 2020-12-01 13:12

    May as well join in on the fun:

    String.prototype.isValidPW = function(){   
        // First, check for at least 8 characters
        if (this.length < 8) return false;
    
        // next, check that we have at least 3 matches
        var re = [/\d/, /[A-Z]/, /[a-z]/, /[!@#$%&\/=?_.,:;-]/], m = 0;
        for (var r = 0; r < re.length; r++){
            if ((this.match(re[r]) || []).length > 0) m++;
        }
    
        return m >= 3;
    };
    
    if ("P@ssW0rd".isValidPW()) alert('Acceptable!');
    

    Demo

    0 讨论(0)
  • 2020-12-01 13:15

    Crimson's answer didn't work for me. Here is what I have.

    var mystring = 'bLahbla\\';
    var valid_char_count = 0;
    var lc = mystring.match(/[a-z]+/);
    var uc = mystring.match(/[A-Z]+/);
    var dc = mystring.match(/[0-9]+/);
    var sc = mystring.match(/[\!\@\#\$\%\&\=\?\_\.\,\:\;\-\\]/);
    
    if( lc ){ valid_char_count++; }
    if( uc ){ valid_char_count++; }
    if( dc ){ valid_char_count++; }
    if( sc ){ valid_char_count++; }
    
    if( valid_char_count >= 3 ){ /* success */ }
    
    0 讨论(0)
  • 2020-12-01 13:24

    I am assuming that you will be using different regexes for different requirements. In that case, tell me if the following work for you:

    var e = password.match(/.{8,}/); //At least 8 chars
    
    var a = password.match(/[0-9]+/); //numeric
    var b = password.match(/[A-Z]+/); //Capitals
    var c = password.match(/[a-z]+/); //small letters
    var d = password.match(/[!@#\$%&/=?_.,:;-\\]+/); //special chars
    
    if (a + b + c + d > 2 && e) {// Success}
    else {// Failure}
    
    0 讨论(0)
  • 2020-12-01 13:24

    http://jsfiddle.net/aSsR8/6/

    /**
     * Function determine, wheter we have valid password
     * 
     * @param {String} value
     * @return {Boolean}
     */
    function isValidPassword(value) {
        // Here we define all our params
        var validLength = 8,
            minSuccess  = 3,
            isNumeric   = + /\d+/.test(value),
            isCapitals  = + /[A-Z]+/.test(value),
            isSmall     = + /[a-z]+/.test(value),
            isSpecial   = + /[!@#$%&\/=\?_\.,:;\-]+/.test(value);
    
        if (value.length < validLength) { // 8 symbols. We don`t need regexp here
            return false;
        }
    
        if (isNumeric + isCapitals  + isSmall + isSpecial < minSuccess) {
            return false;
        }
    
        return true;
    }
    
    
    document.writeln(isValidPassword('abc'));
    document.writeln(isValidPassword('abc123ABC'));
    document.writeln(isValidPassword('abc123!23'));
    
    0 讨论(0)
  • 2020-12-01 13:25

    This will do all that in one regex

    ^.*(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[\d\W])(?=.*[!@#\$%&/=?_\.,:;-\\]).*$

    0 讨论(0)
  • 2020-12-01 13:33

    Just to learn - would this kind of requirement be possible to implement in pure regex?

    That'd make it a rather hard to read (and therefor maintain!) solution, but here it is:

    (?mx)
    ^
    (
      (?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])                # must contain a-z, A-Z and 0-9
      |                                                # OR
      (?=.*[a-z])(?=.*[A-Z])(?=.*[!@\#$%&/=?_.,:;\\-]) # must contain a-z, A-Z and special
      |                                                # OR
      (?=.*[a-z])(?=.*[0-9])(?=.*[!@\#$%&/=?_.,:;\\-]) # must contain a-z, 0-9 and special
      |                                                # OR
      (?=.*[A-Z])(?=.*[0-9])(?=.*[!@\#$%&/=?_.,:;\\-]) # must contain A-Z, 0-9 and special
    )
    .{8,}                                              # at least 8 chars
    $
    

    A (horrible) Javascript demo:

    var pw = "aa$aa1aa";
    if(pw.match(/^((?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])|(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%&\/=?_.,:;\\-])|(?=.*[a-z])(?=.*[0-9])(?=.*[!@#$%&\/=?_.,:;\\-])|(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%&\/=?_.,:;\\-])).{8,}$/)) {
      print('Okay!');
    } else {
      print('Fail...');
    }
    

    prints: Okay!, as you can see on Ideone.

    0 讨论(0)
提交回复
热议问题