English 中文(简体)
如何促使用户进入由资本、小信函、数字和象征组成的强有力的密码?
原标题:how to prompt user to enter a strong password which consists of capital, small letters, numbers and symbol?
public class LockAppActivity extends Activity{

    EditText pass1, pass2;
    Button back, next;
    SharedPreferences prefs;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lockapp);

        pass1 = (EditText) findViewById(R.id.edit1);
        pass2 = (EditText) findViewById(R.id.edit2);

        back = (Button) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {    
            public void onClick(View v1) {
                Intent setIntent = new Intent(Intent.ACTION_MAIN);
                setIntent.addCategory(Intent.CATEGORY_HOME);
                setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(setIntent); 
            }
        });

        next = (Button) findViewById(R.id.next);
        next.setOnClickListener(new View.OnClickListener() {
            String password1 = "";
            String password2 = "";

            public void onClick(View v){

                password1 = pass1.getText().toString();
                password2 = pass2.getText().toString();             

                if (!password1.equals("")){

                    if (password1.length() >= 15){
                        Pattern pattern = Pattern.compile("[[0-9]&&[a-z]&&[A-Z]]");
                        Matcher matcher = pattern.matcher(password1);   

                        if(matcher.matches()){                      

                            if (password1.equals(password2)){
                                //SavePreferences("Password", password1);
                                Intent intent = new Intent(LockAppActivity.this, PhoneNumActivity.class);
                                startActivity(intent);
                            }
                            else{
                                pass1.setText("");
                                pass2.setText("");
                                Toast.makeText(LockAppActivity.this,"Not equal",Toast.LENGTH_LONG).show();
                            }
                        }
                        else{
                            pass1.setText("");
                            pass2.setText("");
                            Toast.makeText(LockAppActivity.this,"Not matched",Toast.LENGTH_LONG).show();
                        }
                    }
                    else{
                        pass1.setText("");
                        pass2.setText("");
                        Toast.makeText(LockAppActivity.this,"Length",Toast.LENGTH_LONG).show();
                    }                       
                }
                else{
                    pass1.setText("");
                    pass2.setText("");
                    Toast.makeText(LockAppActivity.this,"Nothing",Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    private void SavePreferences(String key, String value){
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();
    }
}

我在进入有效密码时面临问题,但是,我pop着一位名叫“不匹配”的传闻,以及我如何在口中挽救这一密码,而新密码进入时,会更新和推翻旧密码。

最佳回答

在Java regex flavor,&&是一个固定的交叉运营商。 它只使用内部品级。 阁下:

[[0-9]&&[a-z]&&[A-Z]]

......力求使一种特性相匹配,该特性必须成为所有三套代码([0-9][A-Z][a-z]的成员。 显然不存在这种性质。 验证密码的最常见办法是使用单独的头盔,与每种所需特性类型相匹配,然后进行“实际”匹配,以验证长度和整体构成。 在你看来,它可能这样做:

^(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])[0-9A-Za-z]{15,}$

您重新使用<代码>matches()方法应用该证书,因此您实际上不必使用起始和终点站(^/code>和_$),但并未造成任何伤害,并且更明确地将你的意图告知必须读到你的代码后的人。

请注意,我只更正你的格格外,以符合你申明的标准,而不评论标准本身。 如果在《守则》的特定部分存在任何错误,我不知道。

问题回答

You can validate the Password using Regualar Expression as below

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PasswordValidator{

  private Pattern pattern;
  private Matcher matcher;

  private static final String PASSWORD_PATTERN = 
          "((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})";

  public PasswordValidator(){
      pattern = Pattern.compile(PASSWORD_PATTERN);
  }

  /**
   * Validate password with regular expression
   * @param password password for validation
   * @return true valid password, false invalid password
   */
  public boolean validate(final String password){

      matcher = pattern.matcher(password);
      return matcher.matches();

  }
}

更多参考:





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...