私が抱えている問題は、パスワードを確認する前に最初のパスワードプロセスを繰り返すことです。Javaパスワードチェック
パスワードは8文字以上でなければなりません。
パスワードが含まれている必要があり、少なくとも:
- 1英字
[a-zA-Z]
- つの数字
[0-9]
英数字ではありません- 1つの文字、など
! # @ $ %^& * () - _ = + [ ] ; : ' " , <.>/?
パスワードは次のいずれでもかまいません。
- はスペース
- はここ感嘆
[!]
または疑問符[?]
で始まる含まれ、それはの一つ私に指示した後、私は取得していますどのようなコード
public static void main(String[] args) {
//declare variables
String inputPassword; // variable for password
String confirmPassword;
// set up input stream from the keyboard
Scanner input = new Scanner (System.in);
// ask for password
System.out.print("Password : ");
inputPassword = input.next();
passCheck(inputPassword);
System.out.print("Please confirm your password : ");
confirmPassword = input.next();
if(inputPassword.matches(confirmPassword)){
System.out.println("Password successfully created.");
} else {
System.out.println("Passwords do not match.");
}
}
public static void passCheck(String password){
boolean valid = true;
if(password.length() < 8){
System.out.println("Password is not eight characters long.");
valid = false;
}
String upperCase = "(.*[A-Z].*)";
if(!password.matches(upperCase)){
System.out.println("Password must contain at least one capital letter.");
valid = false;
}
String numbers = "(.*[0-9].*)";
if(!password.matches(numbers)){
System.out.println("Password must contain at least one number.");
valid = false;
}
String specialChars = "(.*[ ! # @ $ %^& * () - _ = + [ ] ; : ' \" , <.>/?].*)";
if(!password.matches(specialChars)){
System.out.println("Password must contain at least one special character.");
valid = false;
}
String space = "(.*[ ].*)";
if(password.matches(space)){
System.out.println("Password cannot contain a space.");
valid = false;
}
if(password.startsWith("?")){
System.out.println("Password cannot start with '?'.");
valid = false;
}
if(password.startsWith("!")){
System.out.println("Password cannot start with '!'.");
valid = false;
}
if(valid){
System.out.println("Password is valid.");
}
}
があるさ問題は、それは私が欲しいものではないパスワードを確認するように頼むでしょう。
非英数字の場合は '[^ A-Za-z0-9]'を使用できます。 –
は、passCheckからパスワードの有効性(たとえばブール値)を返します。したがって、メインでは、続行する前にブール値をテストできます。 –
パスワードにスペース、疑問符、および感嘆符が含まれているパスワードが強すぎます。 – tbodt