In many cases, it is required that the user's must conform to the rules: including numbers, lowercase English letters, uppercase English letters, one of the special characters ~! @ # $% ^ & and the length must be > = 6
There are many methods, one of which is regular expression.
It's not very convenient to use regular expressions in java. In the end, we still use:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
The specific code is as follows (there is no inspection length here, just whether it contains specific characters):
/** * @author lzf * @created on 2019 2:48:38 PM, October 24, 2010 * @Function to determine whether passwords contain numbers, lowercase letters, uppercase letters, and special symbols (~! @ # $% ^ & *) * @param passwd Clear text password string * @return If both are included, return true, otherwise return false.. */ private boolean isPassWordValid(String passwd){ String regExpS="(?<one>[0-9])|(?<two>[a-z])|(?<four>[A-Z])|(?<three>[~!@#$%^&*_])"; Pattern r = Pattern.compile(regExpS); Matcher isMatch = r.matcher(passwd); boolean isFindNumberOk=false; boolean isFindSmallAlphabetOk=false; boolean isFindBigAlphabetOk=false; boolean isFindSpecialSymblOk=false; while (isMatch.find()){ String one=isMatch.group("one"); if (one!=null && isFindNumberOk==false){ isFindNumberOk=true; } String two=isMatch.group("two"); if (two!=null && isFindSmallAlphabetOk==false){ isFindSmallAlphabetOk=true; } String three=isMatch.group("three"); if (three!=null && isFindSpecialSymblOk==false){ isFindSpecialSymblOk=true; } String four=isMatch.group("four"); if (four!=null && isFindBigAlphabetOk==false){ isFindBigAlphabetOk=true; } //System.out.println(one+"--"+two+"----"+three+"----"+four); } if ( isFindNumberOk && isFindSmallAlphabetOk && isFindBigAlphabetOk && isFindSpecialSymblOk){ return true; //System.out.println("In string["+srcStr+"]Numbers, lowercase letters, uppercase letters, and specific symbols were found in"); } else{ return false; } }
The above code can satisfy the results, but it is not very efficient. In some cases, it is better to analyze strings one by one. However, the business used to verify whether the password input meets the rules usually has low performance requirements, so you can also make do with it!