1. 程式人生 > >Android 點滴記錄

Android 點滴記錄

一:判斷我們輸入的密碼,(長度,大小寫字母,特殊字元)

 private static Pattern pattern1 = Pattern.compile("[a-z]+");
    private static Pattern pattern2 = Pattern.compile("[A-Z]+");
    private static Pattern pattern3 = Pattern.compile("[0-9]+");
    private static Pattern pattern4 = Pattern.compile("\\p{Punct}+");
    private static Pattern pattern5 = Pattern.compile("[a-zA-Z]+.*");

   Matcher matcher1 = pattern1.matcher(password);
        Matcher matcher2 = pattern2.matcher(password);
        Matcher matcher3 = pattern3.matcher(password);
        Matcher matcher4 = pattern4.matcher(password);
        if(password.length()<8||password.length()>20){
            Toast.makeText(context,"密碼長度需設定為8到20位",Toast.LENGTH_SHORT).show();
            return false;
        }
        if(matcher1.find()){
//            Log.e(TAG,"找到小寫字母");
        }else {
            Toast.makeText(context,"密碼需要包含小寫字母",Toast.LENGTH_SHORT).show();
            return false;
        }
        if(matcher2.find()){
//            Log.e(TAG,"找到大寫字母");
        }else {
            Toast.makeText(context,"密碼需要包含大寫字母",Toast.LENGTH_SHORT).show();
            return false;
        }
        if(matcher3.find()){
//            Log.e(TAG,"找到數字");
        }else {
            Toast.makeText(context,"密碼需要包含數字",Toast.LENGTH_SHORT).show();
            return false;
        }
        if(matcher4.find()){
//            Log.e(TAG,"找到特殊字元");
        }else {
            Toast.makeText(context,"密碼需要包含特殊字元",Toast.LENGTH_SHORT).show();
            return false;
        }



 Matcher matcher1 = pattern5.matcher(username);
        if(matcher1.matches()){
//            Log.e(TAG,"找到特殊字元");
        }else {
            Toast.makeText(context,"使用者名稱需要字母開頭",Toast.LENGTH_SHORT).show();
            return false;
        }