0

Im working on javascript regex which includes having following conditions. So far with no luck.

-The minimum character count allowed is 8.

-The maximum character count allowed is 64.

-The entered text should include at least two of the following - numbers, lowercase letters, uppercase letters, Special characters.

-Entering symbols will not be supported.

So far what I have is this @anubhava answer here.

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

This regex will enforce these rules:

-At least one upper case English letter, (?=.*?[A-Z])

-At least one lower case English letter, (?=.*?[a-z])

-At least one digit, (?=.*?[0-9])

-At least one special character, (?=.?[#?!@$%^&-])

-Minimum eight in length .{8,63} (with the anchors)

My Question is how do I satify my 3rd and 4th conditions Which is :-

-The entered text should include at least two of the following - numbers, lowercase letters, uppercase letters, Special characters.

-Entering symbols will not be supported.

Any help would be appreciated.

user2864740
  • 60,010
  • 15
  • 145
  • 220
Selaka Nanayakkara
  • 3,296
  • 1
  • 22
  • 42

3 Answers3

2
^(?!.*[^A-Za-z0-9#?!@$%^&*-]$)(?![a-z]*$)(?![A-Z]*$)(?![0-9]*$)(?![#?!@$%^&*-]*$).{8,64}$

The string should not contain any symbol outside the 4 groups of characters

^(?!.*[^A-Za-z0-9#?!@$%^&*-]$)

The string should not consist only of lower letters

(?![a-z]*$)

The string should not consist only of upper letters

(?![A-Z]*$)

The string should not consist only of digits

(?![0-9]*$)

The string should not consist only of special characters

(?![#?!@$%^&*-]*$)

The string should consist of 8 to 64 characters

.{8,64}$

UPDATED 2020-09-07

If the string should contain symbols of at list 3 groups of 4

^(?!.*[^A-Za-z0-9#?!@$%^&*-]$)((?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])|(?=.*[a-z])(?=.*[A-Z])(?=.*[#?!@$%^&*-])|(?=.*[a-z])(?=.*[0-9])(?=.*[#?!@$%^&*-])|(?=.*[A-Z])(?=.*[0-9])(?=.*[#?!@$%^&*-])).{8,64}$

The string should not contain any symbol outside the 4 groups of characters

^(?!.*[^A-Za-z0-9#?!@$%^&*-]$)

Then 4 variants of 3 groups of 4 that the symbols should be member of:

(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])

or

(?=.*[a-z])(?=.*[A-Z])(?=.*[#?!@$%^&*-])

or

(?=.*[a-z])(?=.*[0-9])(?=.*[#?!@$%^&*-])

or

(?=.*[A-Z])(?=.*[0-9])(?=.*[#?!@$%^&*-])

and finally the string should consist of 8 to 64 characters

.{8,64}$
  • Can you update your answer according to this - The entered text should include at least **three** of the following - numbers, lowercase letters, uppercase letters, Special characters. @J200200831a – Selaka Nanayakkara Sep 07 '20 at 11:25
1

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

 
function test() {

 if(regex.test(document.getElementById("txtPassword").value)===false)
 {
 alert("Min 8,Max 64,At Least One Uppercase Character,One Lowercase Character,One Numeric Value And One Special Character(!@#$%^&*) Required ");
 }
 else
 {
 alert("Success");
 }
}
<input type="text" id="txtPassword" />
<button id="testBtn" onclick=test()>CheckPassword</button>
 
1

Text includes at least two of the following - numbers, lowercase letters, uppercase letters, Special characters. No characters outside [A-Za-z0-9#?!@$%^&*-]

^(?=.*?[A-Z])(?=.*?[a-z])(?!.*?[^A-Za-z0-9#?!@$%^&*-]).{8,63}$|^(?=.*?[A-Z])(?=.*?[0-9])(?!.*?[^A-Za-z0-9#?!@$%^&*-]).{8,63}$|^(?=.*?[A-Z])(?=.*?[#?!@$%^&*-])(?!.*?[^A-Za-z0-9#?!@$%^&*-]).{8,63}$|^(?=.*?[a-z])(?=.*?[0-9])(?!.*?[^A-Za-z0-9#?!@$%^&*-]).{8,63}$|^(?=.*?[a-z])(?=.*?[#?!@$%^&*-])(?!.*?[^A-Za-z0-9#?!@$%^&*-]).{8,63}$|^(?=.*?[0-9])(?=.*?[#?!@$%^&*-])(?!.*?[^A-Za-z0-9#?!@$%^&*-]).{8,63}$

Text includes at least three of the following - numbers, lowercase letters, uppercase letters, Special characters. No characters outside [A-Za-z0-9#?!@$%^&*-]

^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?!.*?[^A-Za-z0-9#?!@$%^&*-]).{8,63}$|^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[#?!@$%^&*-])(?!.*?[^A-Za-z0-9#?!@$%^&*-]).{8,63}$|^(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-])(?!.*?[^A-Za-z0-9#?!@$%^&*-]).{8,63}$|^(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-])(?!.*?[^A-Za-z0-9#?!@$%^&*-]).{8,63}$
NechesStich
  • 196
  • 4