0
import javax.swing.JOptionPane;
class PasswordCreator
{
public PasswordCreator()
{
super();
}
public static void main(String[] args)
{
PasswordCreator passwordCreator = new PasswordCreator();
String userName = JOptionPane.showInputDialog("What is your username?");
String passWord = JOptionPane.showInputDialog("What is your password?");
System.out.println("Input : UserName "+userName+" PassWord -> "+passWord);
passwordCreator.passwordValidation(userName,passWord);
System.out.println();
}
/*
* Password should be less than 15 and more than 8 characters in length.
* Password should contain at least one upper case and one lower case alphabet.
* Password should contain at least one number.
* Password should contain at least one special character.
*/
public void passwordValidation(String userName, String password)
{
boolean valid = true;
if (password.length() > 15 || password.length() < 8)
{
System.out.println("Password should be less than 15 and more than 8 characters in length.");
valid = false;
}
if (password.indexOf(userName) > -1)
{
System.out.println("Password Should not be same as user name");
valid = false;
}
String upperCaseChars = "(.*[A-Z].*)";
if (!password.matches(upperCaseChars))
{
System.out.println("Password should contain atleast one upper case alphabet");
valid = false;
}
String lowerCaseChars = "(.*[a-z].*)";
if (!password.matches(lowerCaseChars))
{
System.out.println("Password should contain atleast one lower case alphabet");
valid = false;
}
String numbers = "(.*[0-9].*)";
if (!password.matches(numbers))
{
System.out.println("Password should contain atleast one number.");
valid = false;
}
String specialChars = "(.*[,~,!,@,#,$,%,^,&,*,(,),-,_,=,+,[,{,],},|,;,:,<,>,/,?].*$)";
if (!password.matches(specialChars))
{
System.out.println("Password should contain atleast one special character");
valid = false;
}
if (valid)
{
System.out.println("Password is valid.");
}
}
}
如果密碼無效,我希望程序要求輸入新密碼。我嘗試過在線搜索,但是我沒有成功地將其實施到我的代碼中。任何人都知道嗎?如何更改此java代碼,以便再次詢問密碼是否無效?
您可以返回有效的布爾結果,然後在運行passwordValidation後詢問結果,如果爲false,則再次詢問密碼。 – 2015-01-26 19:10:02