Password Verification - Add Additional Requirements

I have a code that currently checks the minimum and maximum lentgh. I also want to require uppercase, lowercase, special char, and numeric. Any suggestions on what to add to this, or where can I find some examples? I was Googling and looked through this forum and tried to add additional password requirements and was unsuccessful.

This is what I want to demand.

At least eight characters in length. At most 20 characters in at least lowercase and one uppercase. At least one special character from :! @ # $% ^ & * () ~ `- = _ + [] {} |:"; ',. / <>? at least one number [0-9] Cannot match the login name or email address mail

My current password verification code

public static final int MIN_PASSWORD_LENGTH = 8; public static final int MAX_PASSWORD_LENGTH = 20; public static boolean isAcceptablePassword(String password) { if(TextUtils.isEmpty(password)) return false; int len = password.length(); if(len < MIN_PASSWORD_LENGTH || len > MAX_PASSWORD_LENGTH) return false; for(int i = 0; i < len; i++) { char c = password.charAt(i); if (Character.isWhitespace(c)) return false; } return true; } 
+1
java passwords validation
source share
3 answers

When you parse String data, you must remove the spaces on the right and left. This is done using the Strimg#trim function as follows:

 password = password.trim(); 

To parse each character of a string, you can convert it to a char array, so it will be easier for you to fulfill your requirements:

 char[] arrPassword = password.toCharArray(); 

Now you can evaluate char using the following functions: Character#isUpperCase , Character#isLowerCase , Character#isDigit .

And last but not least, you may have a line with special characters that you need to check and check if the actual character is inside this line. This can be achieved with String#indexOf and String#valueOf , this is las for converting char to String type.

Here is a sample code for all of this explanation:

 public static final String SPECIAL_CHARACTERS = " !@ #$%^&*()~`-=_+[]{}|:\";',./<>?"; public static final int MIN_PASSWORD_LENGTH = 8; public static final int MAX_PASSWORD_LENGTH = 20; public static boolean isAcceptablePassword(String password) { if (TextUtils.isEmpty(password)) { System.out.println("empty string."); return false; } password = password.trim(); int len = password.length(); if(len < MIN_PASSWORD_LENGTH || len > MAX_PASSWORD_LENGTH) { System.out.println("wrong size, it must have at least 8 characters and less than 20."); return false; } char[] aC = password.toCharArray(); for(char c : aC) { if (Character.isUpperCase(c)) { System.out.println(c + " is uppercase."); } else if (Character.isLowerCase(c)) { System.out.println(c + " is lowercase."); } else if (Character.isDigit(c)) { System.out.println(c + " is digit."); } else if (SPECIAL_CHARACTERS.indexOf(String.valueOf(c)) >= 0) { System.out.println(c + " is valid symbol."); } else { System.out.println(c + " is an invalid character in the password."); return false; } } return true; } 

Sentence System.out.println(c + " is an invalid character in the password."); - it's just checking the result of the analysis of the actual character.

+4
source share

What about some good old regular expressions? This seems to work correctly, although it may have slipped away when performing a special char check

 Pattern[] checks = { Pattern.compile("[ !@ #\\$%^&*()~`\\-=_+\\[\\]{}|:\\\";',\\./<>?]"), Pattern.compile("\\d+"), Pattern.compile("[AZ]+"), Pattern.compile("[az]+"), Pattern.compile("^.{8,20}$") }; for (String test : new String[] { "password", "Password1", "Password1&", "toolongtoolongtoolong" }) { boolean ok = true; for (Pattern check : checks) { ok = ok && check.matcher(test).find(); } System.out.println(test + " " + ok); } 
+2
source share

Stephen is right with a few searches, you would easily find your answers here. But the thread Stephen refers to is using a third-party library.

If you want to implement this yourself, then before starting the for-loop, initialize 4 booleans for your requirements with false. With a cyclic check for all four requirements, until true. Set the appropriate boolean to true.

How to check 4 requirements:

  • The length you have already completed.
  • Symbol (yourChar) .isLowerCase ()
  • Symbol (yourChar) .isUpperCase ()
  • Special character: see here Java String Special character replacement - you can choose a similar approach

After the loop, check the 4 logic gates and react accordingly.

0
source share

All Articles