My code asks a user for a password. To properly validate the user's entry, it must include the following requirements:
1)have a length of 5
2)have an uppercase character
3)have a lowercase character
4)have a digitMy loop only works for the lowercase and length of 5 requirements. Can someone help me with validating uppercase and digit? Thanks in advance for any/all suggestions!
import javax.swing.*;
public class pwVer
{
public static void main(String[] args)
{
String input;
input = JOptionPane.showInputDialog("Please enter password");
// Validate user's input
if (isValid(input))
{
JOptionPane.showMessageDialog(null, "Your password is valid!");
}
else
{
JOptionPane.showMessageDialog(null, "Your password is invalid " +
"valid password criteria.");
}
System.exit(0);
}
/**
The isValid method determines password validity.
@param passWord The String to test.
@return true if valid, or else false.
*/
private static boolean isValid(String pw)
{
boolean isvalid = true;
int i = 0;
//Test the length
if (pw.length() != 5)
isvalid = false;
//Test for lowercase
while (isvalid && i < 5)
{
if (!Character.isLowerCase(pw.charAt(i)))
isvalid = false;
i++;
}
//Test for uppercase
while (isvalid && i < 5)
{
if (!Character.isUpperCase(pw.charAt(i)))
isvalid = false;
i++;
}
//Test for digit
while (isvalid && i < 5)
{
if (!Character.isDigit(pw.charAt(i)))
isvalid = false;
i++;
}
return isvalid;
}
}