Regular expressions password how to write?
I need a regular expression to check the password:
Passwords contain at least eight characters, contain at least one number, at the same time contain lowercase and uppercase letters and special characters, such as #
, ?
, !
)
Can not contain the old password or user name, "password"
or"websitename"
Here is my validation expression, used to limit: eight characters, including a capital letter, one lowercase letter and one number or special character.
(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$"
So, how to write a regular, barring password must be eight characters, including a capital letter, a special character and alphanumeric characters?
Best Solutions
At least eight characters, at least one letter and one number:
"^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$"
At least eight characters, at least one letter, one number and one special character:
"^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%*#?&])[A-Za-z\d$@$!%*#?&]{8,}$"
At least eight characters, at least one uppercase letter, one lowercase letter and a number:
"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$"
At least eight characters, at least one uppercase letter, one lowercase letter, one number and one special character:
"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,}"
A minimum of eight up to ten characters, at least one uppercase letter, one lowercase letter, one number and one special character:
"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,10}"
Second best Solutions
You can use this regular expression:
^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$
This regular expression will enforce these rules:
-
At least one capital letter English Letter ,
(?=.*?[A-Z])
-
At least one lower-case letters,
(?=.*?[a-z])
-
At least one digit,
(?=.*?[0-9])
-
At least one special character,
(?=.*?[#?!@$%^&*-])
-
The minimum length of eight
.{8,}
(belt anchor)
Third Solutions
Regular expression is not the AND operator, so write efficient code matching regular expression is quite difficult, especially when the validity is something AND
else such as the definition of the time ...
However, the regular expression does have an OR operator, so just use DeMorgan's theorem, and write a password that matches the regular expression is invalid:
Anything less than eight characters or any digital or no no no no no capital letters or lowercase letters or any string is not any special characters.
and so:
^(.{0,7}|[^0-9]*|[^A-Z]*|[^a-z]*|[a-zA-Z0-9]*)$
If you can match the regular expression, then it is an invalid password.
The fourth idea
One pair of small improvement best answer: Because of the special character keyboard is limited to the special characters, it can be used to express any special characters:
^(?=.*?[A-Z])(?=(.*[a-z]){1,})(?=(.*[\d]){1,})(?=(.*[\W]){1,})(?!.*\s).{8,}$
This regular expression will enforce these rules:
-
At least one uppercase letters
-
At least one lower-case letters
-
At least one digit
-
At least one special character
-
At least eight characters
The fifth idea
In my environment, in accordance with the best answer to do, I had some difficulties. For example, such character ;
or [
authentication fails. I do not want my whitelist list of special characters, so I used [^\w\s]
as a test - just to place a non-matching character (including numbers) and non-space characters. All in all, this approach is more effective for me ...
-
At least
8
characters -
At least
1
numeric character -
At least
1
lowercase letters -
At least
1
in capital letters -
At least
1
special characters
/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[^\w\s]).{8,}$/
Link JSFiddle - simple cover a variety of situations demo
Sixth ideas
Import JavaScript files jquery.validate.min.js
.
You can use the following method:
$.validator.addMethod("pwcheck", function (value) {
return /[\@\#\$\%\^\&\* \_\+\!]/.test(value) && /[a-z]/.test(value) && /[0-9]/.test(value) && /[A-Z]/.test(value) });
This regular expression can be expressed:
-
At least one uppercase letters
-
At least one lower-case letters
-
At least one digit
-
At least one special character
Seventh ideas
Meet the following conditions:
-
At least six characters
-
At least one uppercase character
-
At least one lowercase character
-
At least one special character
Regular Expressions:
"/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&.])[A-Za-z\d$@$!%*?&.]{6, 20}/"
Following conditions are met and there is an optional special characters:
-
At least one special character
-
At least one number
-
Special characters are optional
-
A minimum of six characters, up to 16 characters
Regular Expressions:
"/^(?=.*\d)(?=.*[a-zA-Z]).{6,20}$/"
If no minimum and maximum conditions, you can delete.{6, 16}
-
6 is the minimum number of characters limit
-
20 is the maximum character limit
-
? = Indicates a match expression
Eighth ideas
Conditions need to be met, and the corresponding regular expression:
Conditions: 1] Min 1 uppercase letter.
2] Min 1 lowercase letter.
3] Min 1 special character.
4] Min 1 number.
5] Min 8 characters.
6] Max 30 characters.
Regex: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[#$@!%&*?])[A-Za-z\d#$@!%&*?]{8,30}$/
Ninth ideas
Depending on your needs, the following regular expression should be able to work properly:
^(?=(.*\d){1})(.*\S)(?=.*[a-zA-Z\S])[0-9a-zA-Z\S]{8,}
Just create a string variable, distribution model, and create a Boolean method returns true if the pattern match correctly, otherwise returns false.
Example:
String pattern = "^(?=(.*\d){1})(.*\S)(?=.*[a-zA-Z\S])[0-9a-zA-Z\S]{8,}";
String password_string = "Type the password here"
private boolean isValidPassword(String password_string) { return password_string.matches(Constants.passwordPattern); }
Tenth ideas
Try this regular expression:
^.*(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])[a-zA-Z0-9@#$%^&+=]*$
The regular expression for me perfectly.
function myFunction() {
var str = "c1TTTTaTTT@"; var patt = new RegExp("^.*(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])[a-zA-Z0-9@#$%^&+=]*$"); var res = patt.test(str); console.log("Is regular matches:", res); }