如何在Android中使用密码模式实现密码验证?

时间:2017-06-23 18:54:37

标签: java android validation passwords

我想在此添加密码验证,但在获取它时出现问题,我想要一个密码模式,其中至少包含以下部分内容:

  • 字符数
  • 特殊字符
  • 小写字母
  • 大写字母

但是我无法实现这一点,请帮助。

我正在使用Android Studio,下面是我的Java类文件:

public class SignUp extends MainActivity {
    private EditText et_name, et_email, et_password, et_cpassword;
    private String name, email, password, cpassword;
    Button signupbtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sign_up_form);
    et_name = (EditText) findViewById(R.id.name);
    et_email = (EditText) findViewById(R.id.Email);
    et_password = (EditText) findViewById(R.id.Password);
    et_cpassword = (EditText) findViewById(R.id.Confirm_Password);
    signupbtn = (Button) findViewById(R.id.Signbtn);
    signupbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            register();
        }
    });
}

public void register() {
    initialise();
    if (!validate()) {
        Toast.makeText(this, "Sign up Failed", Toast.LENGTH_SHORT).show();
    } else {
        onSignUpSuccess();
    }
}

public void onSignUpSuccess() {
    Intent intent = new Intent(SignUp.this, HomePage.class);
    startActivity(intent);
    finish();
}

public boolean validate() {
    boolean valid = true;
    if (name.isEmpty() || name.length() > 32) {
        et_name.setError("please enter valid name");
        valid = false;
    }
    if (email.isEmpty() || !Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
        et_email.setError("please enter valid Email");
        valid = false;
    }
    if (password.isEmpty() )
        et_password.setError("please enter a valid password");
    return valid;
}
public void initialise(){
    name = et_name.getText().toString().trim();
    email = et_email.getText().toString().trim();
    password = et_password.getText().toString().trim();
    cpassword = et_cpassword.getText().toString().trim();
}
}

2 个答案:

答案 0 :(得分:0)

您可以使用以下课程。

public class Validation {

        // Regular Expression
        // you can change the expression based on your need
        private static final String EMAIL_REGEX = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
        //private static final String EMAIL_REGEX = "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";
        private static final String PHONE_REGEX = "\\d{3}-\\d{7}";
        private static final String ZIP_REGEX = "\\d{5}";
        // Error Messages
        private static final String REQUIRED_MSG = "required";
        private static final String EMAIL_MSG = "invalid email";
        private static final String PHONE_MSG = "###-#######";
        private static final String ZIP_MSG = "#####";

        public static boolean isEmailAddress(EditText editText, boolean required) {
            return isValid(editText, EMAIL_REGEX, EMAIL_MSG, required);
        }

        public static boolean isPhoneNumber(EditText editText, boolean required) {
            return isValid(editText, PHONE_REGEX, PHONE_MSG, required);
        }

        public static boolean isZip(EditText editText,boolean required){
            return isValid(editText,ZIP_REGEX,ZIP_MSG,required);
        }

        public static boolean isValid(EditText editText, String regex, String errMsg, boolean required) {

            String text = editText.getText().toString().trim();
            editText.setError(null);

            if ( required && !hasText(editText) ) return false;

            if (required && !Pattern.matches(regex, text)) {
                editText.setError(errMsg);
                return false;
            };

            return true;
        }

        public static boolean hasText(EditText editText) {

            String text = editText.getText().toString().trim();
            editText.setError(null);

            if (text.length() == 0) {
                editText.setError(REQUIRED_MSG);
                return false;
            }

            return true;
        }
}

答案 1 :(得分:0)

您正在寻找的模式如下所示:

^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{4,}$

这是一个完整的模式,应该强制用户使用数字,小写,大写和特殊字符。