I have been struggling around for 4 hours but the string in which I am taking my password is not checking the length condition. I know i am doing some stupid mistake but unfortunately I can't figure it out. Finally I decided to ask from Experts here kindly help me with this.
PS: I am writing a function to validate the password to be 4 to 11 characters long,contain one uppercase character,one lowercase character,one digit and one special character.
public void validPassword(){
String password;
boolean con = true;
Pattern[] passRegex = new Pattern[4];
{
passRegex[0] = Pattern.compile(".*[A-Z].*");
passRegex[1] = Pattern.compile(".*[a-z].*");
passRegex[2] = Pattern.compile(".*\\d.*");
passRegex[3] = Pattern.compile("[A-Za-z0-9]*");
}
while(con){
System.out.println("Enter Your Password Using Correct Format:");
password = input.next();
if(password.length() < 4 && password.length() > 11){
System.out.println("Your Password Should Be 4 To 11 Characters Long");
}
if(!passRegex[0].matcher(password).matches()){
System.out.println("Your Password Must Contain Atleast One UpperCase Letter");
}
if(!passRegex[1].matcher(password).matches()){
System.out.println("Your Password Must Contain Atleast One LowerCase Letter");
}
if(!passRegex[2].matcher(password).matches()){
System.out.println("Your Password Must Contain Atleast One Digit");
}
if(passRegex[3].matcher(password).matches()){
System.out.println("Your Password Must Contain Atleast One Special Character");
}
else{
System.out.println("Your Password Is Correct");
con = false;
}
}
}
you need or in your first condition not and
if(password.length() < 4 || password.length() > 11){
not
if(password.length() < 4 && password.length() > 11){
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.