1. 程式人生 > >java身份證號碼校驗、郵箱校驗、手機號碼/電話號碼校驗

java身份證號碼校驗、郵箱校驗、手機號碼/電話號碼校驗

i++ start exceptio span cas table mail pub sys

  1. import java.text.ParseException;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Calendar;
  4. import java.util.GregorianCalendar;
  5. import java.util.Hashtable;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8. public class ValidateUtils {
  9. public static void main(String[] args) throws ParseException {
  10. //已測試通過
  11. System.out.println(validPhoneNum("2","179352031901"));
  12. System.out.println(checkEmail("[email protected]"));
  13. System.out.println(IDCardValidate("15222119880911471X"));
  14. }
  15. /**
  16. *校驗郵箱格式
  17. */
  18. public static boolean checkEmail(String value){
  19. boolean flag=false;
  20. Pattern p1 = null;
  21. Matcher m = null;
  22. p1 = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
  23. m = p1.matcher(value);
  24. flag = m.matches();
  25. return flag;
  26. }
  27. /**
  28. * @param checkType 校驗類型:0校驗手機號碼,1校驗座機號碼,2兩者都校驗滿足其一就可
  29. * @param phoneNum
  30. * */
  31. public static boolean validPhoneNum(String checkType,String phoneNum){
  32. boolean flag=false;
  33. Pattern p1 = null;
  34. Pattern p2 = null;
  35. Matcher m = null;
  36. p1 = Pattern.compile("^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\\d{8})?$");
  37. p2 = Pattern.compile("^(0[0-9]{2,3}\\-)?([1-9][0-9]{6,7})$");
  38. if("0".equals(checkType)){
  39. System.out.println(phoneNum.length());
  40. if(phoneNum.length()!=11){
  41. return false;
  42. }else{
  43. m = p1.matcher(phoneNum);
  44. flag = m.matches();
  45. }
  46. }else if("1".equals(checkType)){
  47. if(phoneNum.length()<11||phoneNum.length()>=16){
  48. return false;
  49. }else{
  50. m = p2.matcher(phoneNum);
  51. flag = m.matches();
  52. }
  53. }else if("2".equals(checkType)){
  54. if(!((phoneNum.length() == 11 && p1.matcher(phoneNum).matches())||(phoneNum.length()<16&&p2.matcher(phoneNum).matches()))){
  55. return false;
  56. }else{
  57. flag = true;
  58. }
  59. }
  60. return flag;
  61. }
  62. /**
  63. * 功能:身份證的有效驗證
  64. */
  65. public static boolean IDCardValidate(String IDStr) throws ParseException {
  66. IDStr = IDStr.trim().toUpperCase();
  67. String errorInfo = "";// 記錄錯誤信息
  68. String[] ValCodeArr = { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" };
  69. String[] Wi = { "7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7", "9", "10", "5", "8", "4", "2" };
  70. String Ai = "";
  71. // ================ 號碼的長度 15位或18位 ================
  72. if (IDStr.length() != 15 && IDStr.length() != 18) {
  73. //身份證號碼長度應該為15位或18位
  74. return false;
  75. }
  76. // =======================(end)========================
  77. // ================ 數字 除最後以為都為數字 ================
  78. if (IDStr.length() == 18) {
  79. Ai = IDStr.substring(0, 17);
  80. } else if (IDStr.length() == 15) {
  81. Ai = IDStr.substring(0, 6) + "19" + IDStr.substring(6, 15);
  82. }
  83. if (isNumeric(Ai) == false) {
  84. //身份證15位號碼都應為數字 ; 18位號碼除最後一位外,都應為數字。
  85. return false;
  86. }
  87. // =======================(end)========================
  88. // ================ 出生年月是否有效 ================
  89. String strYear = Ai.substring(6, 10);// 年份
  90. String strMonth = Ai.substring(10, 12);// 月份
  91. String strDay = Ai.substring(12, 14);// 月份
  92. if (isDataFormat(strYear + "-" + strMonth + "-" + strDay) == false) {
  93. //身份證生日無效。
  94. return false;
  95. }
  96. GregorianCalendar gc = new GregorianCalendar();
  97. SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
  98. if ((gc.get(Calendar.YEAR) - Integer.parseInt(strYear)) > 150
  99. || (gc.getTime().getTime() - s.parse(strYear + "-" + strMonth + "-" + strDay).getTime()) < 0) {
  100. //身份證生日不在有效範圍。
  101. return false;
  102. }
  103. if (Integer.parseInt(strMonth) > 12 || Integer.parseInt(strMonth) == 0) {
  104. //身份證月份無效
  105. return false;
  106. }
  107. if (Integer.parseInt(strDay) > 31 || Integer.parseInt(strDay) == 0) {
  108. //身份證日期無效
  109. return false;
  110. }
  111. // =====================(end)=====================
  112. // ================ 地區碼時候有效 ================
  113. Hashtable h = GetAreaCode();
  114. if (h.get(Ai.substring(0, 2)) == null) {
  115. //身份證地區編碼錯誤。
  116. return false;
  117. }
  118. // ==============================================
  119. // ================ 判斷最後一位的值 ================
  120. int TotalmulAiWi = 0;
  121. for (int i = 0; i < 17; i++) {
  122. TotalmulAiWi = TotalmulAiWi + Integer.parseInt(String.valueOf(Ai.charAt(i))) * Integer.parseInt(Wi[i]);
  123. }
  124. int modValue = TotalmulAiWi % 11;
  125. String strVerifyCode = ValCodeArr[modValue];
  126. Ai = Ai + strVerifyCode;
  127. if (IDStr.length() == 18) {
  128. if (Ai.equals(IDStr) == false) {
  129. //身份證無效,不是合法的身份證號碼
  130. return false;
  131. }
  132. } else {
  133. return true;
  134. }
  135. // =====================(end)=====================
  136. return true;
  137. }
  138. /**
  139. * 功能:設置地區編碼
  140. */
  141. private static Hashtable GetAreaCode() {
  142. Hashtable hashtable = new Hashtable();
  143. hashtable.put("11", "北京");
  144. hashtable.put("12", "天津");
  145. hashtable.put("13", "河北");
  146. hashtable.put("14", "山西");
  147. hashtable.put("15", "內蒙古");
  148. hashtable.put("21", "遼寧");
  149. hashtable.put("22", "吉林");
  150. hashtable.put("23", "黑龍江");
  151. hashtable.put("31", "上海");
  152. hashtable.put("32", "江蘇");
  153. hashtable.put("33", "浙江");
  154. hashtable.put("34", "安徽");
  155. hashtable.put("35", "福建");
  156. hashtable.put("36", "江西");
  157. hashtable.put("37", "山東");
  158. hashtable.put("41", "河南");
  159. hashtable.put("42", "湖北");
  160. hashtable.put("43", "湖南");
  161. hashtable.put("44", "廣東");
  162. hashtable.put("45", "廣西");
  163. hashtable.put("46", "海南");
  164. hashtable.put("50", "重慶");
  165. hashtable.put("51", "四川");
  166. hashtable.put("52", "貴州");
  167. hashtable.put("53", "雲南");
  168. hashtable.put("54", "西藏");
  169. hashtable.put("61", "陜西");
  170. hashtable.put("62", "甘肅");
  171. hashtable.put("63", "青海");
  172. hashtable.put("64", "寧夏");
  173. hashtable.put("65", "新疆");
  174. hashtable.put("71", "臺灣");
  175. hashtable.put("81", "香港");
  176. hashtable.put("82", "澳門");
  177. hashtable.put("91", "國外");
  178. return hashtable;
  179. }
  180. /**
  181. * 驗證日期字符串是否是YYYY-MM-DD格式
  182. */
  183. public static boolean isDataFormat(String str) {
  184. boolean flag = false;
  185. // String
  186. // regxStr="[1-9][0-9]{3}-[0-1][0-2]-((0[1-9])|([12][0-9])|(3[01]))";
  187. String regxStr = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$";
  188. Pattern pattern1 = Pattern.compile(regxStr);
  189. Matcher isNo = pattern1.matcher(str);
  190. if (isNo.matches()) {
  191. flag = true;
  192. }
  193. return flag;
  194. }
  195. /**
  196. * 功能:判斷字符串是否為數字
  197. */
  198. private static boolean isNumeric(String str) {
  199. Pattern pattern = Pattern.compile("[0-9]*");
  200. Matcher isNum = pattern.matcher(str);
  201. if (isNum.matches()) {
  202. return true;
  203. } else {
  204. return false;
  205. }
  206. }
  207. // 身份證號碼驗證:end
  208. }

java身份證號碼校驗、郵箱校驗、手機號碼/電話號碼校驗