在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

字符串验证Java工具类

2016-12-20 13:17| 发布者: zhangjf| 查看: 468| 评论: 0

摘要: /** * 验证模块,包括输入输出的验证 */public class Validator { /** * 判断用户输入的时间格式是否正确 */ public static boolean checkDateTime(String inputDate) { String DATE_TIME_FORM ...
  1. /**
  2. * 验证模块,包括输入输出的验证
  3. */
  4. public class Validator {
  5. /**
  6. * 判断用户输入的时间格式是否正确
  7. */
  8. public static boolean checkDateTime(String inputDate) {
  9. String DATE_TIME_FORMAT = "yyyy-MM-dd";
  10. SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
  11. DATE_TIME_FORMAT, Locale.CHINA);
  12. simpleDateFormat.setLenient(false);
  13. boolean check = false;
  14. try {
  15. simpleDateFormat.parse(inputDate);
  16. check = true;
  17. } catch (Exception ex) {
  18. check = false;
  19. ex.printStackTrace();
  20. }
  21. return check;
  22. }
  23. /**
  24. * 此方法判断输入字符是否为数字0-9 是返回true不是返回false
  25. *
  26. * @param c char
  27. * @return boolean
  28. */
  29. public static boolean isDigit(char c) {
  30. return (('0' <= c) && (c <= '9'));
  31. }
  32. public static boolean isDigit(String inputStr) {
  33. char tempChar;
  34. for (int i = 0; i < inputStr.length(); i++) {
  35. tempChar = inputStr.charAt(i);
  36. // 如果字符中有一个字符不是数字则返回false
  37. if (!isDigit(tempChar)) {
  38. return false;
  39. }
  40. }
  41. return true;
  42. }
  43. /**
  44. * 此方法判断输入字符是否为字母a-z或A-Z 是返回true不是返回false
  45. *
  46. * @param c char
  47. * @return boolean
  48. */
  49. public static boolean isAlpha(char c) {
  50. return ((('a' <= c) && (c <= 'z')) || (('A' <= c) && (c <= 'Z')));
  51. }
  52. public static boolean isAlpha(String inputStr) {
  53. char tempChar;
  54. for (int i = 0; i < inputStr.length(); i++) {
  55. tempChar = inputStr.charAt(i);
  56. if (!isAlpha(tempChar)) { // 如果字符中有一个字符不是字母则返回false
  57. return false;
  58. }
  59. }
  60. return true;
  61. }
  62. /**
  63. * 此方法用于检查密码或用户名是否合法,用户名密码只能使用英文字母、数字以及-和_,并且首字符必须为字母或数字 密码首字符必须为字母或数字
  64. *
  65. * @param inputStr 输入
  66. * @return boolean
  67. */
  68. public static boolean checkUserNamePassword(String inputStr) {
  69. for (int nIndex = 0; nIndex < inputStr.length(); nIndex++) {
  70. char cCheck = inputStr.charAt(nIndex);
  71. if (nIndex == 0 && (cCheck == '-' || cCheck == '_')) {
  72. return false;
  73. }
  74. if (!(isDigit(cCheck) || isAlpha(cCheck) || cCheck == '-' || cCheck == '_')) {
  75. return false;
  76. }
  77. }
  78. return true;
  79. }
  80. /**
  81. * 此方法检查email有效性 返回提示信息
  82. *
  83. * @param email
  84. * @return
  85. */
  86. public static boolean checkEmail(String email) {
  87. // 电子邮件
  88. String check = "^([a-z0-9A-Z]+[-|\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\.)+[a-zA-Z]{2,}$";
  89. Pattern regex = Pattern.compile(check);
  90. Matcher matcher = regex.matcher(email);
  91. boolean isMatched = matcher.matches();
  92. return isMatched;
  93. }
  94. /**
  95. * 此方法检查输入的身份证号有效性 返回提示信息 如果返回布尔类型则通过验证
  96. *
  97. * @param IDNumber 身份证号
  98. * @return String
  99. */
  100. public static boolean checkIDNumber(String IDNumber) {
  101. boolean result = IDNumber.matches("[0-9]{15}|[0-9]{17}[0-9X]");
  102. if (result) {
  103. int year, month, date;
  104. if (IDNumber.length() == 15) {
  105. year = Integer.parseInt(IDNumber.substring(6, 8));
  106. month = Integer.parseInt(IDNumber.substring(8, 10));
  107. date = Integer.parseInt(IDNumber.substring(10, 12));
  108. } else {
  109. year = Integer.parseInt(IDNumber.substring(6, 10));
  110. month = Integer.parseInt(IDNumber.substring(10, 12));
  111. date = Integer.parseInt(IDNumber.substring(12, 14));
  112. }
  113. switch (month) {
  114. case 2:
  115. result = (date >= 1)
  116. && (year % 4 == 0 ? date <= 29 : date <= 28);
  117. break;
  118. case 1:
  119. case 3:
  120. case 5:
  121. case 7:
  122. case 8:
  123. case 10:
  124. case 12:
  125. result = (date >= 1) && (date <= 31);
  126. break;
  127. case 4:
  128. case 6:
  129. case 9:
  130. case 11:
  131. result = (date >= 1) && (date <= 31);
  132. break;
  133. default:
  134. result = false;
  135. break;
  136. }
  137. }
  138. return result;
  139. }
  140. /**
  141. * 1、号码的结构 公民身份号码是特征组合码,由十七位数字本体码和一位校验码组成。排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,
  142. * 三位数字顺序码和一位数字校验码。
  143. *
  144. * 2、地址码(前六位数) 表示编码对象常住户口所在县(市、旗、区)的行政区划代码,按GB/T2260的规定执行。
  145. *
  146. * 3、出生日期码(第七位至十四位) 表示编码对象出生的年、月、日,按GB/T7408的规定执行,年、月、日代码之间不用分隔符。
  147. *
  148. * 4、顺序码(第十五位至十七位)
  149. * 表示在同一地址码所标识的区域范围内,对同年、同月、同日出生的人编定的顺序号,顺序码的奇数分配给男性,偶数分配给女性。
  150. *
  151. * 5、校验码(第十八位数) (1)十七位数字本体码加权求和公式 S = Sum(Ai * Wi), i = 0, , 16
  152. * ,先对前17位数字的权求和 Ai:表示第i位置上的身份证号码数字值 Wi:表示第i位置上的加权因子 Wi: 7 9 10 5 8 4 2 1 6
  153. * 3 7 9 10 5 8 4 2 (2)计算模 Y = mod(S, 11) (3)通过模得到对应的校验码 Y: 0 1 2 3 4 5 6 7
  154. * 8 9 10 校验码: 1 0 X 9 8 7 6 5 4 3 2
  155. *
  156. * */
  157. /**
  158. * 判断手机号码是否合法
  159. *
  160. * @param handset 手机号
  161. * @return 是否合法
  162. */
  163. public static boolean isHandset(String handset) {
  164. try {
  165. if (!handset.substring(0, 1).equals("1")) {
  166. return false;
  167. }
  168. if (handset == null || handset.length() != 11) {
  169. return false;
  170. }
  171. // String check = "^[0123456789]+$";
  172. String check = "^1[3-8]\d{9}$";
  173. Pattern regex = Pattern.compile(check);
  174. Matcher matcher = regex.matcher(handset);
  175. return matcher.matches();
  176. } catch (RuntimeException e) {
  177. e.printStackTrace();
  178. return false;
  179. }
  180. }
  181. /**
  182. * 判断大陆地区固话及小灵通
  183. * 区号:010,020,021,022,023,024,025,027,028,029
  184. *
  185. * @param tel 电话号码
  186. * @return 是否合法
  187. */
  188. public static boolean isTel(String tel) {
  189. try {
  190. String check = "^0(10|2[0-5789]|\d{3})\d{7,8}$";
  191. Pattern regex = Pattern.compile(check);
  192. Matcher matcher = regex.matcher(tel);
  193. return matcher.matches();
  194. } catch (RuntimeException e) {
  195. e.printStackTrace();
  196. return false;
  197. }
  198. }
  199. /**
  200. * 验证是否包含中文
  201. *
  202. * @param str
  203. * @return 是否包含中文:含有中文-true,没有中文-false
  204. */
  205. public static boolean containChinese(String str) {
  206. // String check = "^[u4e00-u9fa5]{1,}$";
  207. String check = "^[\u4e00-\u9fa5]+?";
  208. Pattern regex = Pattern.compile(check);
  209. Matcher matcher = regex.matcher(str);
  210. return matcher.find();
  211. }
  212. /**
  213. * 验证是否包含空格
  214. *
  215. * @param str
  216. * @return 是否包含空格
  217. */
  218. public static boolean containBlank(String str) {
  219. if(str.length() > str.replace(" ", "").length()) {
  220. return true;
  221. } else {
  222. return false;
  223. }
  224. }
  225. /**
  226. * 验证用户名是否只含中英文和数字
  227. *
  228. * @param userName 用户名
  229. * @return 是否合法
  230. */
  231. public static boolean isUserName(String userName) {
  232. String check = "^[\u4E00-\u9FA5A-Za-z0-9]+$";
  233. Pattern regex = Pattern.compile(check);
  234. Matcher matcher = regex.matcher(userName);
  235. return matcher.matches();
  236. }
  237. /**
  238. * 身份证的有效验证
  239. *
  240. * @param IDStr 身份证号
  241. * @return 有效:true 无效:false
  242. * @throws ParseException
  243. */
  244. public boolean IDCardValidate(String IDStr) throws ParseException {
  245. String errorInfo = "";// 记录错误信息
  246. String[] ValCodeArr = {"1", "0", "x", "9", "8", "7", "6", "5", "4",
  247. "3", "2"};
  248. String[] Wi = {"7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7",
  249. "9", "10", "5", "8", "4", "2"};
  250. // String[] Checker = {"1","9","8","7","6","5","4","3","2","1","1"};
  251. String Ai = "";
  252. // ================ 号码的长度 15位或18位 ================
  253. if (IDStr.length() != 15 && IDStr.length() != 18) {
  254. errorInfo = "号码长度应该为15位或18位。";
  255. System.out.println(errorInfo);
  256. return false;
  257. }
  258. // =======================(end)========================
  259. // ================ 数字 除最后以为都为数字 ================
  260. if (IDStr.length() == 18) {
  261. Ai = IDStr.substring(0, 17);
  262. } else if (IDStr.length() == 15) {
  263. Ai = IDStr.substring(0, 6) + "19" + IDStr.substring(6, 15);
  264. }
  265. if (isNumeric(Ai) == false) {
  266. errorInfo = "15位号码都应为数字 ; 18位号码除最后一位外,都应为数字。";
  267. System.out.println(errorInfo);
  268. return false;
  269. }
  270. // =======================(end)========================
  271. // ================ 出生年月是否有效 ================
  272. String strYear = Ai.substring(6, 10);// 年份
  273. String strMonth = Ai.substring(10, 12);// 月份
  274. String strDay = Ai.substring(12, 14);// 月份
  275. if (isDate(strYear + "-" + strMonth + "-" + strDay) == false) {
  276. errorInfo = "生日无效。";
  277. System.out.println(errorInfo);
  278. return false;
  279. }
  280. GregorianCalendar gc = new GregorianCalendar();
  281. SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
  282. if ((gc.get(Calendar.YEAR) - Integer.parseInt(strYear)) > 150
  283. || (gc.getTime().getTime() - s.parse(
  284. strYear + "-" + strMonth + "-" + strDay).getTime()) < 0) {
  285. errorInfo = "生日不在有效范围。";
  286. System.out.println(errorInfo);
  287. return false;
  288. }
  289. if (Integer.parseInt(strMonth) > 12 || Integer.parseInt(strMonth) == 0) {
  290. errorInfo = "月份无效";
  291. System.out.println(errorInfo);
  292. return false;
  293. }
  294. if (Integer.parseInt(strDay) > 31 || Integer.parseInt(strDay) == 0) {
  295. errorInfo = "日期无效";
  296. System.out.println(errorInfo);
  297. return false;
  298. }
  299. // =====================(end)=====================
  300. // ================ 地区码时候有效 ================
  301. Hashtable<String, String> h = GetAreaCode();
  302. if (h.get(Ai.substring(0, 2)) == null) {
  303. errorInfo = "地区编码错误。";
  304. System.out.println(errorInfo);
  305. return false;
  306. }
  307. // ==============================================
  308. // ================ 判断最后一位的值 ================
  309. int TotalmulAiWi = 0;
  310. for (int i = 0; i < 17; i++) {
  311. TotalmulAiWi = TotalmulAiWi
  312. + Integer.parseInt(String.valueOf(Ai.charAt(i)))
  313. * Integer.parseInt(Wi[i]);
  314. }
  315. int modValue = TotalmulAiWi % 11;
  316. String strVerifyCode = ValCodeArr[modValue];
  317. Ai = Ai + strVerifyCode;
  318. if (IDStr.length() == 18) {
  319. if (Ai.equals(IDStr) == false) {
  320. errorInfo = "身份证无效,最后一位字母错误";
  321. System.out.println(errorInfo);
  322. return false;
  323. }
  324. } else {
  325. System.out.println("所在地区:" + h.get(Ai.substring(0, 2).toString()));
  326. System.out.println("新身份证号:" + Ai);
  327. return true;
  328. }
  329. // =====================(end)=====================
  330. System.out.println("所在地区:" + h.get(Ai.substring(0, 2).toString()));
  331. return true;
  332. }
  333. /**
  334. * ======================================================================
  335. * 功能:设置地区编码
  336. *
  337. * @return Hashtable 对象
  338. */
  339. private Hashtable<String, String> GetAreaCode() {
  340. Hashtable<String, String> hashtable = new Hashtable<String, String>();
  341. hashtable.put("11", "北京");
  342. hashtable.put("12", "天津");
  343. hashtable.put("13", "河北");
  344. hashtable.put("14", "山西");
  345. hashtable.put("15", "内蒙古");
  346. hashtable.put("21", "辽宁");
  347. hashtable.put("22", "吉林");
  348. hashtable.put("23", "黑龙江");
  349. hashtable.put("31", "上海");
  350. hashtable.put("32", "江苏");
  351. hashtable.put("33", "浙江");
  352. hashtable.put("34", "安徽");
  353. hashtable.put("35", "福建");
  354. hashtable.put("36", "江西");
  355. hashtable.put("37", "山东");
  356. hashtable.put("41", "河南");
  357. hashtable.put("42", "湖北");
  358. hashtable.put("43", "湖南");
  359. hashtable.put("44", "广东");
  360. hashtable.put("45", "广西");
  361. hashtable.put("46", "海南");
  362. hashtable.put("50", "重庆");
  363. hashtable.put("51", "四川");
  364. hashtable.put("52", "贵州");
  365. hashtable.put("53", "云南");
  366. hashtable.put("54", "西藏");
  367. hashtable.put("61", "陕西");
  368. hashtable.put("62", "甘肃");
  369. hashtable.put("63", "青海");
  370. hashtable.put("64", "宁夏");
  371. hashtable.put("65", "新疆");
  372. hashtable.put("71", "台湾");
  373. hashtable.put("81", "香港");
  374. hashtable.put("82", "澳门");
  375. hashtable.put("91", "国外");
  376. return hashtable;
  377. }
  378. /**
  379. * ======================================================================
  380. * 功能:判断字符串是否为数字
  381. *
  382. * @param str
  383. * @return
  384. */
  385. private boolean isNumeric(String str) {
  386. Pattern pattern = Pattern.compile("[0-9]*");
  387. Matcher isNum = pattern.matcher(str);
  388. if (isNum.matches()) {
  389. return true;
  390. } else {
  391. return false;
  392. }
  393. }
  394. /**
  395. * 功能:判断字符串是否为日期格式
  396. *
  397. * @param strDate 字符串
  398. * @return
  399. */
  400. public boolean isDate(String strDate) {
  401. Pattern pattern = Pattern
  402. .compile("^((\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])))))?$");
  403. Matcher m = pattern.matcher(strDate);
  404. if (m.matches()) {
  405. return true;
  406. } else {
  407. return false;
  408. }
  409. }
  410. /**
  411. * ======================================================================
  412. * =功能:在判定已经是正确的身份证号码之后,查找出身份证所在地区
  413. *
  414. * @param idCard 身份证号码
  415. * @return 所在地区
  416. */
  417. public String GetArea(String idCard) {
  418. Hashtable<String, String> ht = GetAreaCode();
  419. String area = ht.get(idCard.substring(0, 2));
  420. return area;
  421. }
  422. /**
  423. * ======================================================================
  424. * =功能:在判定已经是正确的身份证号码之后,查找出此人性别
  425. *
  426. * @param idCard 身份证号码
  427. * @return 男或者女
  428. */
  429. public String GetSex(String idCard) {
  430. String sex = "";
  431. if (idCard.length() == 15)
  432. sex = idCard.substring(idCard.length() - 3, idCard.length());
  433. if (idCard.length() == 18)
  434. sex = idCard.substring(idCard.length() - 4, idCard.length() - 1);
  435. System.out.println(sex);
  436. int sexNum = Integer.parseInt(sex) % 2;
  437. if (sexNum == 0) {
  438. return "女";
  439. }
  440. return "男";
  441. }
  442. /**
  443. * ======================================================================
  444. * =功能:在判定已经是正确的身份证号码之后,查找出此人出生日期
  445. *
  446. * @param idCard 身份证号码
  447. * @return 出生日期 XXXX MM-DD
  448. */
  449. public String GetBirthday(String idCard) {
  450. String Ain = "";
  451. if (idCard.length() == 18) {
  452. Ain = idCard.substring(0, 17);
  453. } else if (idCard.length() == 15) {
  454. Ain = idCard.substring(0, 6) + "19" + idCard.substring(6, 15);
  455. }
  456. // ================ 出生年月是否有效 ================
  457. String strYear = Ain.substring(6, 10);// 年份
  458. String strMonth = Ain.substring(10, 12);// 月份
  459. String strDay = Ain.substring(12, 14);// 日期
  460. return strYear + "-" + strMonth + "-" + strDay;
  461. }
  462. }
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-8 15:35

Copyright 2015-2025 djqfx

返回顶部