Android 密码校验,字符串中必须包含字母或者数字

来源:互联网 发布:苹果打碟软件 编辑:程序博客网 时间:2024/05/16 06:56

下面是Util类的内容提供了三种密码规则的校验和一个判断输入类型的方法

[java] view plain copy
  1. /** 
  2.  * Created by zhangshun on 2016/8/18. 
  3.  */  
  4. public class PwdCheckUtil {  
  5.   
  6.     /** 
  7.      * 规则1:至少包含大小写字母及数字中的一种 
  8.      * 是否包含 
  9.      * 
  10.      * @param str 
  11.      * @return 
  12.      */  
  13.     public static boolean isLetterOrDigit(String str) {  
  14.         boolean isLetterOrDigit = false;//定义一个boolean值,用来表示是否包含字母或数字  
  15.         for (int i = 0; i < str.length(); i++) {  
  16.             if (Character.isLetterOrDigit(str.charAt(i))) {   //用char包装类中的判断数字的方法判断每一个字符  
  17.                 isLetterOrDigit = true;  
  18.             }  
  19.         }  
  20.         String regex = "^[a-zA-Z0-9]+$";  
  21.         boolean isRight = isLetterOrDigit && str.matches(regex);  
  22.         return isRight;  
  23.     }  
  24.   
  25.     /** 
  26.      * 规则2:至少包含大小写字母及数字中的两种 
  27.      * 是否包含 
  28.      * 
  29.      * @param str 
  30.      * @return 
  31.      */  
  32.     public static boolean isLetterDigit(String str) {  
  33.         boolean isDigit = false;//定义一个boolean值,用来表示是否包含数字  
  34.         boolean isLetter = false;//定义一个boolean值,用来表示是否包含字母  
  35.         for (int i = 0; i < str.length(); i++) {  
  36.             if (Character.isDigit(str.charAt(i))) {   //用char包装类中的判断数字的方法判断每一个字符  
  37.                 isDigit = true;  
  38.             } else if (Character.isLetter(str.charAt(i))) {  //用char包装类中的判断字母的方法判断每一个字符  
  39.                 isLetter = true;  
  40.             }  
  41.         }  
  42.         String regex = "^[a-zA-Z0-9]+$";  
  43.         boolean isRight = isDigit && isLetter && str.matches(regex);  
  44.         return isRight;  
  45.     }  
  46.   
  47.     /** 
  48.      * 规则3:必须同时包含大小写字母及数字 
  49.      * 是否包含 
  50.      * 
  51.      * @param str 
  52.      * @return 
  53.      */  
  54.     public static boolean isContainAll(String str) {  
  55.         boolean isDigit = false;//定义一个boolean值,用来表示是否包含数字  
  56.         boolean isLowerCase = false;//定义一个boolean值,用来表示是否包含字母  
  57.         boolean isUpperCase = false;  
  58.         for (int i = 0; i < str.length(); i++) {  
  59.             if (Character.isDigit(str.charAt(i))) {   //用char包装类中的判断数字的方法判断每一个字符  
  60.                 isDigit = true;  
  61.             } else if (Character.isLowerCase(str.charAt(i))) {  //用char包装类中的判断字母的方法判断每一个字符  
  62.                 isLowerCase = true;  
  63.             } else if (Character.isUpperCase(str.charAt(i))) {  
  64.                 isUpperCase = true;  
  65.             }  
  66.         }  
  67.         String regex = "^[a-zA-Z0-9]+$";  
  68.         boolean isRight = isDigit && isLowerCase && isUpperCase && str.matches(regex);  
  69.         return isRight;  
  70.     }  
  71.   
  72.     /** 
  73.      *   判断EditText输入的数字、中文还是字母方法 
  74.      */  
  75.     public static void whatIsInput(Context context, EditText edInput) {  
  76.         String txt = edInput.getText().toString();  
  77.   
  78.         Pattern p = Pattern.compile("[0-9]*");  
  79.         Matcher m = p.matcher(txt);  
  80.         if (m.matches()) {  
  81.             Toast.makeText(context, "输入的是数字", Toast.LENGTH_SHORT).show();  
  82.         }  
  83.         p = Pattern.compile("[a-zA-Z]");  
  84.         m = p.matcher(txt);  
  85.         if (m.matches()) {  
  86.             Toast.makeText(context, "输入的是字母", Toast.LENGTH_SHORT).show();  
  87.         }  
  88.         p = Pattern.compile("[\u4e00-\u9fa5]");  
  89.         m = p.matcher(txt);  
  90.         if (m.matches()) {  
  91.             Toast.makeText(context, "输入的是汉字", Toast.LENGTH_SHORT).show();  
  92.         }  
  93.     }  
  94. }  
阅读全文
0 0