0

ユーザー入力が英数字のみであるかどうかをチェックし、基準に基づいて入力されたパスワードの強度をチェックするコードがあります。次の場合、メモ付きのラベルが表示されます。

-フィールドが空です

-無効な文字が使用されています

また、パスワードがWEAK、MEDIUM、STRONG、 VERY STRONGの場合も表示されます

this.AlphaNumericOnly(this.jTextField1.getText(),this.warningLbl3);
this.passwordStrength(this.jTextField1.getText(),this.warningLbl4);

このメソッドは、jtextfield で使用すると正常に機能します。ただし、jpasswordfield を使用してユーザー入力を非表示にしたいと考えています。

私はすでに試しました:

.toString();

String.valueOf();

そしてこのループ:

char[] input=(this.jPasswordField1.getPassword());
 final_pass = "";
    for(char x : input) {
     final_pass += x;
    }

しかし、メソッドは変換した文字列を「チェック」できません。

ここに私の方法があります..

public void AlphaNumericOnly(String input,JLabel obj){
if(!"".equals(input)){
    obj.setText(" ");
    warning=false;

    char c[] = input.toCharArray();

    int count = 0;
    for(int x=0; x<c.length; x++){
        if((!Character.isAlphabetic(c[x]))&&(!Character.isDigit(c[x]))){// && c[x]!='-' && c[x]!='(' && c[x]!=')' && c[x]!='+'&& c[x]!='/'&& c[x]!='\\'){
            count=+1;        
        }
    }
    if(count>0){
        obj.setText("*Please use valid characters.");
        warning=true;
    }else{
        obj.setText(" ");
        warning=false;
    }
}else{
    obj.setText("*This Field cannot be left Empty.");
    warning=true;
}   
}
private void passwordStrength(String input,JLabel obj){
   char c[] = input.toCharArray();
   int count=0;
   int notAlphaNumericCount=0;
   for(int j=0;j<c.length;j++){
       if(Character.isAlphabetic(c[j])){
            if(Character.isUpperCase(c[j])){
                count++;
            }
       }else if(Character.isDigit(c[j])){

       }else{
         obj.setText(" ");
         warning=false;
         notAlphaNumericCount++;
       }
   }
if(notAlphaNumericCount==0){   
    if(input.length()<1){
        obj.setText(" ");
        warning=false;
    }else if(input.length()<4){
        obj.setText("Password Strength: WEAK");
        obj.setForeground(Color.red);
    }else if(input.length()<8){
        obj.setText("Password Strength: MEDIUM");
        obj.setForeground(Color.blue);
    }else if(input.length()<10){
        obj.setText("Password Strength: STRONG");
        obj.setForeground(Color.green);
    }else if(count!=0){    
        obj.setText("Password Strength: VERY STRONG");
        obj.setForeground(Color.orange);
    }
}
}

編集: より視覚的に理解するために、このメソッドの使用方法を次に示します。

username [Textfield input]- [obj label warning]
password [Textfield input]- [obj label warning]

          [obj label which displays password strength]
4

1 に答える 1

1

まあ、getPassword()関数の戻り値はchar配列なので。コンストラクターを介して簡単に文字列に変換できます。

new String(this.jPasswordField1.getPassword());

passwordStrengthただし、個人的には、メソッドの引数を String の代わりに char 配列を取るように変更した方がよいと思います。引数inputを使用して char 配列に変換し、その長さを取得するためです。また、この方法で String を構築するたびに、新しい String オブジェクトを作成します。

于 2015-08-06T09:46:54.063 に答える