您好,登錄后才能下訂單哦!
本文實例講述了Android編程實現的身份證、車牌號正則驗證工具類。分享給大家供大家參考,具體如下:
/** * 正則表達式驗證工具類(驗證身份證、車牌號等) * * @author chenlin * */ public class ValidateUtil { /** * 驗證str是否為正確的身份證格式 * * @param str * @return */ public static boolean isIdentityCard(EditText view) { boolean flag = true; String licenc = view.getText().toString(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); /* * { 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:"國外" } */ 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 = Pattern.compile("^[1-9]\\d{14}"); Matcher matcher = pattern.matcher(licenc); Pattern pattern2 = Pattern.compile("^[1-9]\\d{16}[\\d,x,X]$"); Matcher matcher2 = pattern2.matcher(licenc); // 粗略判斷 if (!matcher.find() && !matcher2.find()) { view.setError("身份證號必須為15或18位數字(最后一位可以為X)"); flag = false; } else { // 判斷出生地 if (provinces.indexOf(licenc.substring(0, 2)) == -1) { view.setError("身份證號前兩位不正確!"); flag = false; } // 判斷出生日期 if (licenc.length() == 15) { String birth = "19" + licenc.substring(6, 8) + "-" + licenc.substring(8, 10) + "-" + licenc.substring(10, 12); try { Date birthday = sdf.parse(birth); if (!sdf.format(birthday).equals(birth)) { view.setError("出生日期非法!"); flag = false; } if (birthday.after(new Date())) { view.setError("出生日期不能在今天之后!"); flag = false; } } catch (ParseException e) { view.setError("出生日期非法!"); flag = false; } } else if (licenc.length() == 18) { String birth = licenc.substring(6, 10) + "-" + licenc.substring(10, 12) + "-" + licenc.substring(12, 14); try { Date birthday = sdf.parse(birth); if (!sdf.format(birthday).equals(birth)) { view.setError("出生日期非法!"); flag = false; } if (birthday.after(new Date())) { view.setError("出生日期不能在今天之后!"); flag = false; } } catch (ParseException e) { view.setError("出生日期非法!"); flag = false; } } else { view.setError("身份證號位數不正確,請確認!"); flag = false; } } if (!flag) { view.requestFocus(); } return flag; } /** * 不為空時,驗證str是否為正確的身份證格式 * * @param str * @return */ public static boolean maybeIsIdentityCard(EditText view) { boolean flag = true; String licenc = view.getText().toString(); if (!licenc.equals("")) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); /* * { 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:"國外" } */ 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 = Pattern.compile("^[1-9]\\d{14}"); Matcher matcher = pattern.matcher(licenc); Pattern pattern2 = Pattern.compile("^[1-9]\\d{16}[\\d,x,X]$"); Matcher matcher2 = pattern2.matcher(licenc); // 粗略判斷 if (!matcher.find() && !matcher2.find()) { view.setError("身份證號必須為15或18位數字(最后一位可以為X)"); flag = false; } else { // 判斷出生地 if (provinces.indexOf(licenc.substring(0, 2)) == -1) { view.setError("身份證號前兩位不正確!"); flag = false; } // 判斷出生日期 if (licenc.length() == 15) { String birth = "19" + licenc.substring(6, 8) + "-" + licenc.substring(8, 10) + "-" + licenc.substring(10, 12); try { Date birthday = sdf.parse(birth); if (!sdf.format(birthday).equals(birth)) { view.setError("出生日期非法!"); flag = false; } if (birthday.after(new Date())) { view.setError("出生日期不能在今天之后!"); flag = false; } } catch (ParseException e) { view.setError("出生日期非法!"); flag = false; } } else if (licenc.length() == 18) { String birth = licenc.substring(6, 10) + "-" + licenc.substring(10, 12) + "-" + licenc.substring(12, 14); try { Date birthday = sdf.parse(birth); if (!sdf.format(birthday).equals(birth)) { view.setError("出生日期非法!"); flag = false; } if (birthday.after(new Date())) { view.setError("出生日期不能在今天之后!"); flag = false; } } catch (ParseException e) { view.setError("出生日期非法!"); flag = false; } } else { view.setError("身份證號位數不正確,請確認!"); flag = false; } } if (!flag) { view.requestFocus(); } } return flag; } /** * 驗證str是否為正確的身份證格式 * * @param str * @return */ public static boolean isIdentityCard(String licenc) { boolean flag = true; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); /* * { 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:"國外" } */ 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 = Pattern.compile("^[1-9]\\d{14}"); Matcher matcher = pattern.matcher(licenc); Pattern pattern2 = Pattern.compile("^[1-9]\\d{16}[\\d,x,X]$"); Matcher matcher2 = pattern2.matcher(licenc); // 粗略判斷 if (!matcher.find() && !matcher2.find()) { flag = false; } else { // 判斷出生地 if (provinces.indexOf(licenc.substring(0, 2)) == -1) { flag = false; } // 判斷出生日期 if (licenc.length() == 15) { String birth = "19" + licenc.substring(6, 8) + "-" + licenc.substring(8, 10) + "-" + licenc.substring(10, 12); try { Date birthday = sdf.parse(birth); if (!sdf.format(birthday).equals(birth)) { flag = false; } if (birthday.after(new Date())) { flag = false; } } catch (ParseException e) { flag = false; } } else if (licenc.length() == 18) { String birth = licenc.substring(6, 10) + "-" + licenc.substring(10, 12) + "-" + licenc.substring(12, 14); try { Date birthday = sdf.parse(birth); if (!sdf.format(birthday).equals(birth)) { flag = false; } if (birthday.after(new Date())) { flag = false; } } catch (ParseException e) { flag = false; } } else { flag = false; } } return flag; } /** * 驗證str是否為正確的車牌號 * * @param str * @return */ public static boolean isPlateNo(EditText view) { String no = view.getText().toString().trim(); if (no == null || no.equals("")) { return false; } String str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; String[] str1 = { "京", "津", "冀", "晉", "蒙", "遼", "吉", "黑", "滬", "蘇", "浙", "皖", "閩", "贛", "魯", "豫", "鄂", "湘", "粵", "桂", "瓊", "渝", "川", "貴", "云", "藏", "陜", "甘", "青", "寧", "新", "農", "臺", "中", "武", "WJ", "亥", "戌", "酉", "申", "未", "午", "巳", "辰", "卯", "寅", "丑", "子", "葵", "壬", "辛", "庚", "己", "戊", "丁", "丙", "乙", "甲", "河北", "山西", "北京", "北", "南", "蘭", "沈", "濟", "成", "廣", "海", "空", "軍", "京V", "使" }; if (no.equals("新車")) { return true; } if (no.length() == 7) { int h = 0; for (int r = 0; r < no.length(); r++) { if (str.indexOf(no.charAt(r)) != -1) { h++; } } if (h == 7) { return true; } } if (no.length() > 1) { String jq1 = no.substring(0, 1); String jq2 = no.substring(0, 2); for (int k = 0; k < str1.length; k++) { if (str1[k].equals(jq1)) { if (no.length() <= 8) { return true; } } if (str1[k].equals(jq2)) { if (no.length() <= 8) { return true; } } } } return false; } public static boolean isEmpty(TextView w, String displayStr) { if (StringUtil.empty(w.getText().toString().trim())) { w.setError(displayStr + "不能為空!"); w.setFocusable(true); w.requestFocus(); return true; } return false; } public static boolean isNum(TextView w, String displayStr) { if (!StringUtil.num(w.getText().toString().trim())) { w.setError(displayStr + "必須為整數且大于0!"); w.setFocusable(true); w.requestFocus(); 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 = Pattern.compile(strPattern); Matcher m = p.matcher(strEmail); return m.matches(); } public static boolean isDouble(TextView w, String displayStr) { if (!StringUtil.decimal(w.getText().toString().trim())) { w.setError(displayStr + "必須為數字且大于0!"); w.setFocusable(true); w.requestFocus(); return true; } return false; } // 可以空,非空時必須正確 public static boolean maybeAccountNumberEmpty(TextView w) { if (!StringUtil.empty(w.getText().toString().trim())) { return ValidateUtil.isAccountNumber(w); } return true; } // 可以空,非空時必須正確 public static boolean maybeMobileEmpty(TextView w) { if (!StringUtil.empty(w.getText().toString().trim())) { return ValidateUtil.isMobileNumber(w); } return true; } public static boolean isArea(TextView w) { if (!RegexUtil.isArea(w.getText().toString().trim())) { w.setError("面積有非法字符!"); w.setFocusable(true); return false; } return true; } public static boolean isMobileNumber(TextView w) { if (!RegexUtil.isMobileNumber(w.getText().toString().trim())) { w.setError("手機號碼為11位數字!"); w.setFocusable(true); return false; } return true; } /** 銀行帳號為16-21位的數字 */ public static boolean isAccountNumber(TextView w) { if (!RegexUtil.isAccountNumber(w.getText().toString().trim())) { w.setError("銀行帳號必須為16-21位的數字!"); w.setFocusable(true); return false; } return true; } }
PS:這里再為大家提供2款非常方便的正則表達式工具供大家參考使用:
JavaScript正則表達式在線測試工具:
http://tools.jb51.net/regex/javascript
正則表達式在線生成工具:
http://tools.jb51.net/regex/create_reg
更多關于Android相關內容感興趣的讀者可查看本站專題:《Android控件用法總結》、《Android開發入門與進階教程》、《Android視圖View技巧總結》、《Android編程之activity操作技巧總結》、《Android數據庫操作技巧總結》及《Android資源操作技巧匯總》
希望本文所述對大家Android程序設計有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。