I need to create a regex to validate password.
It must contain at least 8 characters, which must have at least 1 letter, 1 number and 1 special character.
I need to create a regex to validate password.
It must contain at least 8 characters, which must have at least 1 letter, 1 number and 1 special character.
Used Regex
This regex does the validation, you can test it here :
(?=.*[}{,.^?~=+\-_\/*\-+.\|])(?=.*[a-zA-Z])(?=.*[0-9]).{8,}
Remember that you should use double escape where it has the character \
, so in Java code all backslashes must be double, getting \
.
Java application
In the Java code you can validate with String.matches
, as I showed below:
public class JavaFiddle
{
public static void main(String[] args)
{
System.out.println("A{123456".matches("(?=.*[}{,.^?~=+\-_\/*\-+.\|])(?=.*[a-zA-Z])(?=.*[0-9]).{8,}")); //exemplo que passa
System.out.println("ASD1ASDA".matches("(?=.*[}{,.^?~=+\-_\/*\-+.\|])(?=.*[a-zA-Z])(?=.*[0-9]).{8,}")); //exemplo que nao passa (falta caractere especial)
System.out.println("ASDAS^^?".matches("(?=.*[}{,.^?~=+\-_\/*\-+.\|])(?=.*[a-zA-Z])(?=.*[0-9]).{8,}")); //exemplo que nao passa (falta numero)
System.out.println("123^8542".matches("(?=.*[}{,.^?~=+\-_\/*\-+.\|])(?=.*[a-zA-Z])(?=.*[0-9]).{8,}")); //exemplo que nao passa (falta letra)
System.out.println("WWEA^^1".matches("(?=.*[}{,.^?~=+\-_\/*\-+.\|])(?=.*[a-zA-Z])(?=.*[0-9]).{8,}")); //exemplo que nao passa (nao tem minimo de 8 caracteres)
}
}
This code displays true
on the console when it is a valid pattern, and false
for invalid patterns.
You can test this code in the JavaFiddle as I did too, just copy and paste the code from that answer.
NOTE: As the special characters were not specified, I used the following characters ( ,.^?~=+-_/*\+
) as valid
You can change the characters that enter as special characters in this part of the regex:
(?=.*[}{,.^?~=+\-_\/*\-+.\|])
, just do not remove the part (?=
... )
, as this signals a positive lookahead , which is required to validate that there is at least one of the characters flagged in the group before starting the capture.
Explanation of Regex
(?=.*
... )
- is a positive lookahead that will ensure that the following character set MUST be in the string for it to be validated. [}{,.^?~=+\-_\/*\-+.\|]
- This is the set that defines which characters can be captured by positive lookahead . (?=.*
... )
- is a positive lookahead that will ensure that the following character set MUST be in the string for it to be validated. [a-zA-Z]
- This is the set that characterizes any letter character being uppercase or lowercase. (?=.*
... )
- is a positive lookahead that will ensure that the following character set MUST be in the string for it to be validated. [0-9]
- Is the set that characterizes any number between 0 and 9. .{8,}
- After these validations, a sequence of any character that does not contain line break (flagged by .
) will be captured. And that is a minimum length of 8 characters (signaled by {8,}
), without maximum limit (if you want to set a limit you can put the maximum character number after ,
). EDITED
Thanks to the user @hkotsubo for telling me about my error