开发者

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

开发者 https://www.devze.com 2023-03-04 17:07 出处:网络
I\'m trying to write a password vali开发者_运维问答dator. How can I see if my supplied string contains at least 3 different character groups?

I'm trying to write a password vali开发者_运维问答dator.

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

It's easy enough to check if they are existant or not ---but at least 3?

  • at least eight (8) characters

  • At least three different character groups

    upper-case letter

    lower-case letter

    numeric

    special characters !@#$%&/=?_.,:;-\

(I'm using javascript for regex)


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.


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


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}


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'));


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 */ }


This will do all that in one regex

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

0

精彩评论

暂无评论...
验证码 换一张
取 消