I Have To Make A Regex And Cannot Figure Out The Syntax
The rules of the regex are: be at least 8 characters, use at least one of the following: (&%$^#@=), have at least 2 numbers that are not at either the beginning or the end NOR
Solution 1:
This tested JavaScript function includes a commented regex which meets your validation requirements:
functionvalid_password(text) {
/*#!(?#!js re_password Rev:20160409_0700)
# Password validation with multiple requirements.
^ # Anchor to start of string.
(?=[^&%$^#@=]*[&%$^#@=]) # Use at least one (&%$^#@=).
(?=(?:\D*\d){2}) # Have at least 2 numbers.
(?!\d) # Numbers not at the beginning.
(?!.*\d$) # Numbers not at the end.
(?=\D*(?:\d\D+)+$) # Numbers not next to each other.
(?=(?:[^A-Z]*[A-Z]){2}) # At least 2 uppercase.
(?=(?:[^a-z]*[a-z]){2}) # At least 2 lowercase.
.{8,} # Be at least 8 characters.
$ # Anchor to end of string.
!#*/var re_password = /^(?=[^&%$^#@=]*[&%$^#@=])(?=(?:\D*\d){2})(?!\d)(?!.*\d$)(?=\D*(?:\d\D+)+$)(?=(?:[^A-Z]*[A-Z]){2})(?=(?:[^a-z]*[a-z]){2}).{8,}$/;
if (text.match(re_password)) { returntrue; }
returnfalse;
}
Note that this regex demonstrates how multiple AND logic conditions may be applied at the position located at the beginning of the string. Note also that the multiple conditions for the numbers requirements in the regex above could be consolidated into a single assertion - they are presented here in verbose (multiple assertion style), to demonstrate the technique used.
Hope this helps!
Solution 2:
This regex seems to work
(?=^.{8,}$)(?!^\d)(?!.*\d$)(?!.+\d\d)(?=.*[&%$^#@=])(?=(.*[A-Z]){2})(?=(.*[a-z]){2})(?=(.*[0-9]){2})
Regex Breakdown
(?=^.{8,}$)#Positive look ahead to check whether there are at least 8 characters
(?!^\d) #Negative look ahead to check that string does not begin with a digit
(?!.*\d$)#Negative look ahead to check that string does not end with a digit
(?!.+\d\d) #Negative look ahead to check that string does not have two consecutive digits
(?=.*[&%$^#@=]) #Positive look ahead to check that string have at least any of the characters present in character class
(?=(.*[A-Z]){2}) #Positive look ahead to check that string contains at least two Upper Case characters
(?=(.*[a-z]){2}) #Positive look ahead to check that string contains at least two Lower Case characters
(?=(.*[0-9]){2}) #Positive look ahead to check that string contains at least two digits
Post a Comment for "I Have To Make A Regex And Cannot Figure Out The Syntax"