7

Regex Password complexity requires that any three of the following four characteristics must be applied when creating or changing a password.

  • Alpha characters - at least 1 upper case alpha character
  • Alpha characters - at least 1 lower case alpha character
  • Numeric characters - at least 1 numeric character
  • Special characters - at least 1 special character

I am trying with the following code, but its not working for special characters

(?=^.{6,}$)((?=.*\d)(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[^A-Za-z0-9])(?=.*[a-z])|(?=.*[^A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9]))^.*

I want my regex to be validated against the following 4 cases

Match cases

  • P@ssword
  • Password1
  • p@ssword1
  • p@12345
1

7 Answers 7

11

I think that a regex you can use is:

(?=^.{6,}$)(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[^A-Za-z0-9]).*

I'm not sure why you have so many or operators in your regex but this one matches if:

  • (?=^.{6,}$) - String is > 5 chars
  • (?=.*[0-9]) - Contains a digit
  • (?=.*[A-Z]) - Contains an uppercase letter
  • (?=.*[a-z]) - Contains a lowercase letter
  • (?=.*[^A-Za-z0-9]) - A character not being alphanumeric.

Regular expression image

Sign up to request clarification or add additional context in comments.

2 Comments

But i need any one of the three options, not all the 4.
@sivanv Oh, but isn't your regex working? See this. And you can change the password one at a time to check the other ones.
7

I think a single regex will be messy in this case. You can easily do something like

var count = 0;

count += /[a-z]/.test(password) ? 1 : 0;
count += /[A-Z]/.test(password) ? 1 : 0;
count += /\d/.test(password) ? 1 : 0;
count += /[@]/.test(password) ? 1 : 0;

if(count > 2) {
    alert('valid')
}

5 Comments

Can I do it with help of regex, thats the requirement. :( I already achieved it using the above way, but need it in plain Regex.
This is regex, but multiple of them.
@sivanv you must also learn, that not always the solution is hammer, even if the problem at the beginning looks like a nail
a single regex here will be too messy and a hell to maintain later
I think you could simplify a bit: count += +/[a-z]/.test(password) same with the others right? Or simply: [/[a-z]/,/[A-Z]/,/\d/,/\W/].filter(function(t){ return t.test(pass); }).length > 2
2

Use this Regex :

(?=^.{6,10}$)(?=.\d)(?=.[a-z])(?=.[A-Z])(?=.[!@#$%^&*()_+}{":;'?/>.<,])(?!.\s).$**

1 Comment

not working even the fiddle, could you check again?, even the / is not escaped how would this even work ?
0

I think you would need this for all special characters too : [updated to reject space]

    $(document).ready(function(){
    $('input').change(function(){
    var count = 0;
    var pass = $(this).val();
        count += /[a-z]/.test(pass) ? 1 : 0;
        count += /[A-Z]/.test(pass) ? 1 : 0;
        count += /\d/.test(pass) ? 1 : 0;
        count += /[^\w\d\s]/.test(pass) ? 1 : 0;
        (count>2 & !/[\s]+/.test(pass)) ? $(this).css('background-color','lime'):$(this).css('background-color','orange');
    });

});

and the fiddle : jsFiddle

Comments

0
var pattern = new RegExp(/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]).{6,}$/);

    if(pattern.test(value)){
         return true;
    } else {
         return false;
    }

Its working fine with special character also.

Comments

0

Non English UTF-8

None of the solutions given allow international letters, i.e. éÉöÖæÆáÁ, but mainly focus on the english ASCII alphabet.

The following regEx uses unicode, UTF-8, to recognise upper and lower case and thus, allow international characters:

// Match uppercase, lowercase, digit or #$!%*?& and make sure the length is 6 to 36 in length  
const pwdFilter = /^(?=.*\p{Ll})(?=.*\p{Lu})(?=.*[\d|@#$!%*?&])[\p{L}\d@#$!%*?&]{6,36}$/gu

if (!pwdFilter.test(pwd)) {
    // Show error that password has to be adjusted to match criteria
}

The regEx:

/^(?=.*\p{Ll})(?=.*\p{Lu})(?=.*[\d|@#$!%*?&])[\p{L}\d@#$!%*?&]{6,36}$/gmu

checks if an uppercase, lowercase, digit or @#$!%*?& are used in the password. It also limits the length to be 6 minimum and maximum 36 (note that emojis, 😀🇺🇸🇪🇸🧑‍💻, count as more than one character in the length). The u in the end, is for using UTF-8.

Comments

-1

To match for special simply list down all the special characters there are in a lookahead. See code below.

const pattern1 = /(?=\D)/; // non-digit character match
const pattern2 = /(?=.*[~`!@#$%^&*()--+={}\[\]|\\:;"'<>,.?/_₹])/; // at least one special char exists

Alternatively, assumingly, as your regex pattern already search for digits and alphabets such as in pattern 3, the only things not matching is non-digits, which are your special characters. Hence you can find match for non-digits characters too! Like in pattern 4.

  const pattern3 =
    /^(?=.*\d)(?=.*[~`!@#$%^&*()--+={}\[\]|\\:;"'<>,.?/_₹])(?=.*[a-z])(?=.*[A-Z]).{5,10}$/;
  const pattern4 =
    /^(?=.*\d)(?=.*\D)(?=.*[a-z])(?=.*[A-Z]).{5,10}$/;

For detailed explanation of the syntax or lookahead, you may read up the article below.

https://medium.com/p/19fc2b2a299c

2 Comments

Please add some more explanation to your answer such that others can learn from it. Also, share all explanation in your answer instead of posting links to external ressources
Thanks for the feedback, I have reviewed my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.