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
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
- possible duplicate of RegEx question for password strength validation – Toto Commented Jun 14, 2013 at 13:08
7 Answers
Reset to default 11I 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.
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')
}
Use this Regex :
(?=^.{6,10}$)(?=.\d)(?=.[a-z])(?=.[A-Z])(?=.[!@#$%^&*()_+}{":;'?/>.<,])(?!.\s).$**
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
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.
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,