A regular expression that allows at least one special character, one uppercase, one lowercase (in any order)

Can someone help me with a regex to allow at least one special character, one uppercase, one lowercase.

This is what I have so far:

^.*(?=.{8,})(?=.*[az])(?=.*[AZ])(?=.*[@#$%^&+=]).*$ 

but it seems to match characters only in the order of "special character", "upper case", "lower case".

Any help is appreciated

+6
source share
3 answers

Your regular expression

 ^.*(?=.{8,})(?=.*[az])(?=.*[AZ])(?=.*[@#$%^&+=]).*$ 

should work fine, but you can do it much better by removing the first .* :

 ^(?=.{8,})(?=.*[az])(?=.*[AZ])(?=.*[@#$%^&+=]).*$ 

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

+14
source

Here is a function you can use.

 function checkRegex(string) { var checkSpecial = /[*@!#%&()^~{}]+/.test(string), checkUpper = /[AZ]+/.test(string), checkLower = /[az]+/.test(string), r = false; if (checkUpper && checkLower && checkSpecial) { r = true; } return r; } 

and then check if this is true or false.

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

If var regex is true , then the condition is true .

+3
source

This can be done using 3 regular expressions.

 function check($string){ return preg_match("/[`!%$&^*()]+/", $string) && preg_match("/[az]+/", $string) && preg_match("/[AZ]+/", $string) ; } 

Remember to customize the list of special characters. Because I don’t know which characters you think are special.

I believe that spending a lot of time on a single line regular expression until you are an expert will increase your productivity. This tri-mode solution will be great. It saves time.

+2
source

Source: https://habr.com/ru/post/915529/


All Articles