Androidパスワードチェック、文字列にアルファベットまたは数字を含める必要があります

3168 ワード

次はUtilクラスの内容で、3つのパスワードルールの検証と入力タイプを判断する方法を提供します.
/**
 * Created by zhangshun on 2016/8/18.
 */
public class PwdCheckUtil {

    /**
     *   1:                
     *     
     *
     * @param str
     * @return
     */
    public static boolean isLetterOrDigit(String str) {
        boolean isLetterOrDigit = false;//    boolean ,             
        for (int i = 0; i < str.length(); i++) {
            if (Character.isLetterOrDigit(str.charAt(i))) {   // char                   
                isLetterOrDigit = true;
            }
        }
        String regex = "^[a-zA-Z0-9]+$";
        boolean isRight = isLetterOrDigit && str.matches(regex);
        return isRight;
    }

    /**
     *   2:                
     *     
     *
     * @param str
     * @return
     */
    public static boolean isLetterDigit(String str) {
        boolean isDigit = false;//    boolean ,          
        boolean isLetter = false;//    boolean ,          
        for (int i = 0; i < str.length(); i++) {
            if (Character.isDigit(str.charAt(i))) {   // char                   
                isDigit = true;
            } else if (Character.isLetter(str.charAt(i))) {  // char                   
                isLetter = true;
            }
        }
        String regex = "^[a-zA-Z0-9]+$";
        boolean isRight = isDigit && isLetter && str.matches(regex);
        return isRight;
    }

    /**
     *   3:              
     *     
     *
     * @param str
     * @return
     */
    public static boolean isContainAll(String str) {
        boolean isDigit = false;//    boolean ,          
        boolean isLowerCase = false;//    boolean ,          
        boolean isUpperCase = false;
        for (int i = 0; i < str.length(); i++) {
            if (Character.isDigit(str.charAt(i))) {   // char                   
                isDigit = true;
            } else if (Character.isLowerCase(str.charAt(i))) {  // char                   
                isLowerCase = true;
            } else if (Character.isUpperCase(str.charAt(i))) {
                isUpperCase = true;
            }
        }
        String regex = "^[a-zA-Z0-9]+$";
        boolean isRight = isDigit && isLowerCase && isUpperCase && str.matches(regex);
        return isRight;
    }

    /**
     *     EditText     、        
     */
    public static void whatIsInput(Context context, EditText edInput) {
        String txt = edInput.getText().toString();

        Pattern p = Pattern.compile("[0-9]*");
        Matcher m = p.matcher(txt);
        if (m.matches()) {
            Toast.makeText(context, "      ", Toast.LENGTH_SHORT).show();
        }
        p = Pattern.compile("[a-zA-Z]");
        m = p.matcher(txt);
        if (m.matches()) {
            Toast.makeText(context, "      ", Toast.LENGTH_SHORT).show();
        }
        p = Pattern.compile("[\u4e00-\u9fa5]");
        m = p.matcher(txt);
        if (m.matches()) {
            Toast.makeText(context, "      ", Toast.LENGTH_SHORT).show();
        }
    }
}