Password Validate 8 Digits, Contains Upper, Lowercase, and a Special Character

Password validate 8 digits, contains upper, lowercase, and a special character

You should have clearly mention your requirement I was not aware of your requirement. Please find my below solution`

public static void main(String[] args) {

Scanner in = new Scanner(System.in);
System.out.print("Please enter a given password : ");
String passwordhere = in.nextLine();
System.out.print("Please re-enter the password to confirm : ");
String confirmhere = in.nextLine();

List<String> errorList = new ArrayList<String>();

while (!isValid(passwordhere, confirmhere, errorList)) {
System.out.println("The password entered here is invalid");
for (String error : errorList) {
System.out.println(error);
}

System.out.print("Please enter a given password : ");
passwordhere = in.nextLine();
System.out.print("Please re-enter the password to confirm : ");
confirmhere = in.nextLine();
}
System.out.println("your password is: " + passwordhere);

}

public static boolean isValid(String passwordhere, String confirmhere, List<String> errorList) {

Pattern specailCharPatten = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
Pattern UpperCasePatten = Pattern.compile("[A-Z ]");
Pattern lowerCasePatten = Pattern.compile("[a-z ]");
Pattern digitCasePatten = Pattern.compile("[0-9 ]");
errorList.clear();

boolean flag=true;

if (!passwordhere.equals(confirmhere)) {
errorList.add("password and confirm password does not match");
flag=false;
}
if (passwordhere.length() < 8) {
errorList.add("Password lenght must have alleast 8 character !!");
flag=false;
}
if (!specailCharPatten.matcher(passwordhere).find()) {
errorList.add("Password must have atleast one specail character !!");
flag=false;
}
if (!UpperCasePatten.matcher(passwordhere).find()) {
errorList.add("Password must have atleast one uppercase character !!");
flag=false;
}
if (!lowerCasePatten.matcher(passwordhere).find()) {
errorList.add("Password must have atleast one lowercase character !!");
flag=false;
}
if (!digitCasePatten.matcher(passwordhere).find()) {
errorList.add("Password must have atleast one digit character !!");
flag=false;
}

return flag;

}

Regular expression to check if password is 8 characters including 1 uppercase letter, 1 special character, alphanumeric characters

The regular expression you are after will most likely be huge and a nightmare to maintain especially for people who are not that familiar with regular expressions.

I think it would be easier to break your regex down and do it one bit at a time. It might take a bit more to do, but I am pretty sure that maintaining it and debugging it would be easier. This would also allow you to provide more directed error messages to your users (other than just Invalid Password) which should improve user experience.

From what I am seeing you are pretty fluent in regex, so I would presume that giving you the regular expressions to do what you need would be futile.

Seeing your comment, this is how I would go about it:

  • Must be eight characters Long: You do not need a regex for this. Using the .Length property should be enough.

  • Including one uppercase letter: You can use the [A-Z]+ regular expression. If the string contains at least one upper case letter, this regular expression will yield true.

  • One special character: You can use either the \W which will match any character which is not a letter or a number or else, you can use something like so [!@#] to specify a custom list of special characters. Note though that characters such as $, ^, ( and ) are special characters in the regular expression language, so they need to be escaped like so: \$. So in short, you might use the \W.

  • Alphanumeric characters: Using the \w+ should match any letter and number and underscore.

Take a look at this tutorial for more information.

How to validate a password with upper, lower and special character?

Instead of Special character you can check if your password contains digit. May be for special character you can use char.IsSymbol

if 
(
newPass.Length >= 6 && //if length is >= 6
newPass.Any(char.IsUpper) && //if any character is upper case
newPass.Any(char.IsSymbol) //better to have a digit then Symbol
)
{
//valid
}

Javascript regular expression password validation having special characters

Use positive lookahead assertions:

var regularExpression = /^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{6,16}$/;

Without it, your current regex only matches that you have 6 to 16 valid characters, it doesn't validate that it has at least a number, and at least a special character. That's what the lookahead above is for.

  • (?=.*[0-9]) - Assert a string has at least one number;
  • (?=.*[!@#$%^&*]) - Assert a string has at least one special character.


Related Topics



Leave a reply



Submit