regex to allow atleast one special character, one uppercase, one lowercase(in any order)

Your regex

^.*(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$

should actually work just fine, but you can make it a lot better by removing the first .*:

^(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$

will match any string of at least 8 characters that contains at least one lowercase and one uppercase ASCII character and also at least one character from the set @#$%^&+= (in any order).


Here is a function you can use.

function checkRegex(string) {
    var checkSpecial = /[*@!#%&()^~{}]+/.test(string),
        checkUpper = /[A-Z]+/.test(string),
        checkLower = /[a-z]+/.test(string),
        r = false;

        if (checkUpper && checkLower && checkSpecial) {
            r = true;
        }

        return r;

        }

and then check if it's true or false.

var thisVal = document.getElementById('password').value;
var regex = checkRegex(thisVal);

If var regex is true then the condition satisfied.


It can be done quickly by 3 regular expression.

function check($string){
   return    preg_match("/[`!%$&^*()]+/", $string) 
          && preg_match("/[a-z]+/", $string) 
          && preg_match("/[A-Z]+/", $string) ;
}

Dont forget to tweak the list of special characters. Because I dont know what characters you think are special.

I believe wasting a lot of time on a single line regex while you are not expert will not increase your productivity. This 3 regex solution will do just fine. It saves time.

Tags:

Regex