SoFunction
Updated on 2025-04-10

An example of the regular verification tool for ID card and license plate number implemented by Android programming

This article describes the regular verification tools for ID card and license plate numbers implemented by Android programming. Share it for your reference, as follows:

/**
  * Regular expression verification tool class (verify ID card, license plate number, etc.)
  *
  * @author chenlin
  *
  */
public class ValidateUtil {
 /**
   * Verify that str is the correct ID card format
   *
   * @param str
   * @return
   */
 public static boolean isIdentityCard(EditText view) {
  boolean flag = true;
  String licenc = ().toString();
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  /*
    * { 11:"Beijing", 12:"Tianjin", 13:"Hebei", 14:"Shanxi", 15:"Inner *",
    * 21:"Liaoning", 22:"Jilin", 23:"Heilongjiang", 31:"Shanghai", 32:"Jiangsu",
    * 33:"Zhejiang",34:"Anhui",35:"Fujian",36:"Jiangxi",37:"Shandong",41:"Henan",
    * 42: "Hubei", 43: "Hunan", 44: "Guangdong", 45: "Guangxi", 46: "Hainan", 50: "Chongqing",
    * 51:"Sichuan", 52:"Guizhou", 53:"Yunnan", 54:"Tibet", 61:"Shaanxi", 62:"Gansu",
    * 63:"Qinghai", 64:"Ningxia", 65:"*", 71:"*", 81:"*", 82:"Macao", 91:"Foreign" }
    */
  String provinces = "11,12,13,14,15,21,22,23,31,32,33,34,35,36,37,41,42,43,44,45,46,50,51,52,53,54,61,62,63,64,65,71,81,82,91";
  Pattern pattern = ("^[1-9]\\d{14}");
  Matcher matcher = (licenc);
  Pattern pattern2 = ("^[1-9]\\d{16}[\\d,x,X]$");
  Matcher matcher2 = (licenc);
  // Rough judgment  if (!() && !()) {
   ("The ID number must be 15 or 18 digits (the last digit can be X)");
   flag = false;
  } else {
   //Judge birthplace   if (((0, 2)) == -1) {
    ("The first two digits of the ID number are incorrect!");
    flag = false;
   }
   //Judge date of birth   if (() == 15) {
    String birth = "19" + (6, 8) + "-"
      + (8, 10) + "-"
      + (10, 12);
    try {
     Date birthday = (birth);
     if (!(birthday).equals(birth)) {
      ("The date of birth is illegal!");
      flag = false;
     }
     if ((new Date())) {
      ("The date of birth cannot be after today!");
      flag = false;
     }
    } catch (ParseException e) {
     ("The date of birth is illegal!");
     flag = false;
    }
   } else if (() == 18) {
    String birth = (6, 10) + "-"
      + (10, 12) + "-"
      + (12, 14);
    try {
     Date birthday = (birth);
     if (!(birthday).equals(birth)) {
      ("The date of birth is illegal!");
      flag = false;
     }
     if ((new Date())) {
      ("The date of birth cannot be after today!");
      flag = false;
     }
    } catch (ParseException e) {
     ("The date of birth is illegal!");
     flag = false;
    }
   } else {
    ("The number of ID cards is incorrect, please confirm!");
    flag = false;
   }
  }
  if (!flag) {
   ();
  }
  return flag;
 }
 /**
   * When not empty, verify whether str is the correct ID card format
   *
   * @param str
   * @return
   */
 public static boolean maybeIsIdentityCard(EditText view) {
  boolean flag = true;
  String licenc = ().toString();
  if (!("")) {
   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
   /*
     * { 11:"Beijing", 12:"Tianjin", 13:"Hebei", 14:"Shanxi", 15:"Inner *",
     * 21:"Liaoning", 22:"Jilin", 23:"Heilongjiang", 31:"Shanghai", 32:"Jiangsu",
     * 33:"Zhejiang",34:"Anhui",35:"Fujian",36:"Jiangxi",37:"Shandong",41:"Henan",
     * 42: "Hubei", 43: "Hunan", 44: "Guangdong", 45: "Guangxi", 46: "Hainan", 50: "Chongqing",
     * 51:"Sichuan", 52:"Guizhou", 53:"Yunnan", 54:"Tibet", 61:"Shaanxi", 62:"Gansu",
     * 63:"Qinghai", 64:"Ningxia", 65:"*", 71:"*", 81:"*", 82:"Macao", 91:"Foreign" }
     */
   String provinces = "11,12,13,14,15,21,22,23,31,32,33,34,35,36,37,41,42,43,44,45,46,50,51,52,53,54,61,62,63,64,65,71,81,82,91";
   Pattern pattern = ("^[1-9]\\d{14}");
   Matcher matcher = (licenc);
   Pattern pattern2 = ("^[1-9]\\d{16}[\\d,x,X]$");
   Matcher matcher2 = (licenc);
   // Rough judgment   if (!() && !()) {
    ("The ID number must be 15 or 18 digits (the last digit can be X)");
    flag = false;
   } else {
    //Judge birthplace    if (((0, 2)) == -1) {
     ("The first two digits of the ID number are incorrect!");
     flag = false;
    }
    //Judge date of birth    if (() == 15) {
     String birth = "19" + (6, 8) + "-"
       + (8, 10) + "-"
       + (10, 12);
     try {
      Date birthday = (birth);
      if (!(birthday).equals(birth)) {
       ("The date of birth is illegal!");
       flag = false;
      }
      if ((new Date())) {
       ("The date of birth cannot be after today!");
       flag = false;
      }
     } catch (ParseException e) {
      ("The date of birth is illegal!");
      flag = false;
     }
    } else if (() == 18) {
     String birth = (6, 10) + "-"
       + (10, 12) + "-"
       + (12, 14);
     try {
      Date birthday = (birth);
      if (!(birthday).equals(birth)) {
       ("The date of birth is illegal!");
       flag = false;
      }
      if ((new Date())) {
       ("The date of birth cannot be after today!");
       flag = false;
      }
     } catch (ParseException e) {
      ("The date of birth is illegal!");
      flag = false;
     }
    } else {
     ("The number of ID cards is incorrect, please confirm!");
     flag = false;
    }
   }
   if (!flag) {
    ();
   }
  }
  return flag;
 }
 /**
   * Verify that str is the correct ID card format
   *
   * @param str
   * @return
   */
 public static boolean isIdentityCard(String licenc) {
  boolean flag = true;
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  /*
    * { 11:"Beijing", 12:"Tianjin", 13:"Hebei", 14:"Shanxi", 15:"Inner *",
    * 21:"Liaoning", 22:"Jilin", 23:"Heilongjiang", 31:"Shanghai", 32:"Jiangsu",
    * 33:"Zhejiang",34:"Anhui",35:"Fujian",36:"Jiangxi",37:"Shandong",41:"Henan",
    * 42: "Hubei", 43: "Hunan", 44: "Guangdong", 45: "Guangxi", 46: "Hainan", 50: "Chongqing",
    * 51:"Sichuan", 52:"Guizhou", 53:"Yunnan", 54:"Tibet", 61:"Shaanxi", 62:"Gansu",
    * 63:"Qinghai", 64:"Ningxia", 65:"*", 71:"*", 81:"*", 82:"Macao", 91:"Foreign" }
    */
  String provinces = "11,12,13,14,15,21,22,23,31,32,33,34,35,36,37,41,42,43,44,45,46,50,51,52,53,54,61,62,63,64,65,71,81,82,91";
  Pattern pattern = ("^[1-9]\\d{14}");
  Matcher matcher = (licenc);
  Pattern pattern2 = ("^[1-9]\\d{16}[\\d,x,X]$");
  Matcher matcher2 = (licenc);
  // Rough judgment  if (!() && !()) {
   flag = false;
  } else {
   //Judge birthplace   if (((0, 2)) == -1) {
    flag = false;
   }
   //Judge date of birth   if (() == 15) {
    String birth = "19" + (6, 8) + "-"
      + (8, 10) + "-"
      + (10, 12);
    try {
     Date birthday = (birth);
     if (!(birthday).equals(birth)) {
      flag = false;
     }
     if ((new Date())) {
      flag = false;
     }
    } catch (ParseException e) {
     flag = false;
    }
   } else if (() == 18) {
    String birth = (6, 10) + "-"
      + (10, 12) + "-"
      + (12, 14);
    try {
     Date birthday = (birth);
     if (!(birthday).equals(birth)) {
      flag = false;
     }
     if ((new Date())) {
      flag = false;
     }
    } catch (ParseException e) {
     flag = false;
    }
   } else {
    flag = false;
   }
  }
  return flag;
 }
 /**
   * Verify that str is the correct license plate number
   *
   * @param str
   * @return
   */
 public static boolean isPlateNo(EditText view) {
  String no = ().toString().trim();
  if (no == null || ("")) {
   return false;
  }
  String str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  String[] str1 = { "Beijing", "Tianjin", "Hebei", "Jin", "*n", "Liao", "lucky", "black", "Shanghai", "Su",
    "Zhejiang", "Anhui", "Fu", "Gan", "Lu", "Hey", "E", "Xiang", "Guangdong", "Gui", "Qiong", "Chongqing",
    "Sichuan", "expensive", "cloud", "*", "Shaanxi", "sweet", "green", "rather", "new", "Farmer", "tower", "middle",
    "Martial Arts", "WJ", "Hai", "Xu", "unitary", "state", "not yet", "noon", "Si", "Chen", "South", "Yin",
    "ugly", "son", "sunflower", "the ninth of the ten Heavenly Stems", "pungent", "Geng", "One", "E", "Man", "C", "Second", "First",
    "Hebei", "Shanxi", "Beijing", "north", "South", "orchid", "Shen", "Ji", "become", "wide", "ocean", "null",
    "military", "Beijing V", "make" };
  if (("New Car")) {
   return true;
  }
  if (() == 7) {
   int h = 0;
   for (int r = 0; r < (); r++) {
    if (((r)) != -1) {
     h++;
    }
   }
   if (h == 7) {
    return true;
   }
  }
  if (() > 1) {
   String jq1 = (0, 1);
   String jq2 = (0, 2);
   for (int k = 0; k < ; k++) {
    if (str1[k].equals(jq1)) {
     if (() <= 8) {
      return true;
     }
    }
    if (str1[k].equals(jq2)) {
     if (() <= 8) {
      return true;
     }
    }
   }
  }
  return false;
 }
 public static boolean isEmpty(TextView w, String displayStr) {
  if ((().toString().trim())) {
   (displayStr + "Can't be empty!");
   (true);
   ();
   return true;
  }
  return false;
 }
 public static boolean isNum(TextView w, String displayStr) {
  if (!(().toString().trim())) {
   (displayStr + " Must be an integer and greater than 0!");
   (true);
   ();
   return true;
  }
  return false;
 }
 public static boolean isEmail(String strEmail) {
  String strPattern = "^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
  Pattern p = (strPattern);
  Matcher m = (strEmail);
  return ();
 }
 public static boolean isDouble(TextView w, String displayStr) {
  if (!(().toString().trim())) {
   (displayStr + " Must be a number and greater than 0!");
   (true);
   ();
   return true;
  }
  return false;
 }
 // Can be empty, but must be correct when it is not empty public static boolean maybeAccountNumberEmpty(TextView w) {
  if (!(().toString().trim())) {
   return (w);
  }
  return true;
 }
 // Can be empty, but must be correct when it is not empty public static boolean maybeMobileEmpty(TextView w) {
  if (!(().toString().trim())) {
   return (w);
  }
  return true;
 }
 public static boolean isArea(TextView w) {
  if (!(().toString().trim())) {
   ("There are illegal characters in the area!");
   (true);
   return false;
  }
  return true;
 }
 public static boolean isMobileNumber(TextView w) {
  if (!(().toString().trim())) {
   ("The mobile phone number is 11 digits!");
   (true);
   return false;
  }
  return true;
 }
 /** Numbers with bank account number 16-21 */
 public static boolean isAccountNumber(TextView w) {
  if (!(().toString().trim())) {
   ("The bank account must be 16-21 digits!");
   (true);
   return false;
  }
  return true;
 }
}

PS: Here are two very convenient regular expression tools for your reference:

JavaScript regular expression online testing tool:
http://tools./regex/javascript

Regular expression online generation tool:
http://tools./regex/create_reg

For more information about Android related content, please check out the topic of this site:Android control usage summary》、《Android development introduction and advanced tutorial》、《Android View View Tips Summary》、《Android programming activity operation skills summary》、《Android database operation skills summary"and"Android resource operation skills summary

I hope this article will be helpful to everyone's Android programming design.