CheckUtil.java 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package com.sysu.admin.utils;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4. /**
  5. * 表单验证工具类
  6. * @author 86137
  7. *
  8. */
  9. public class CheckUtil {
  10. /**
  11. * 验证是不是数字
  12. * @param text
  13. * @return
  14. */
  15. public static boolean isNumber(String text) {
  16. if(text == null || text.length() == 0) {
  17. return false;
  18. }
  19. int i = text.length() - 1;
  20. while(i > -1) {
  21. int c = text.charAt(i);
  22. if(c < '0' || c > '9' ) {
  23. return false;
  24. }
  25. i--;
  26. }
  27. return true;
  28. }
  29. public static void main(String[] args) {
  30. System.out.println();
  31. }
  32. /**
  33. * 验证不为null 或 "" 或 " "
  34. * @param text
  35. * @return
  36. */
  37. public static boolean notNull(String text) {
  38. if(text == null || text.trim().equals("")) {
  39. return false;
  40. }
  41. return true;
  42. }
  43. public static boolean isPhone(String phone) {
  44. String regex = "^((13[0-9])|(14[0-9])|(15([0-9]))|(16([0-9]))|(17[0-9])|(18[0-9])|(19[0-9]))\\d{8}$";
  45. if (phone.length() != 11) {
  46. // System.out.println("手机号应为11位数");
  47. return false;
  48. }
  49. Pattern p = Pattern.compile(regex);
  50. Matcher m = p.matcher(phone);
  51. boolean isMatch = m.matches();
  52. if (isMatch) {
  53. // System.out.println("您的手机号" + phone + "是正确格式@——@");
  54. return true;
  55. } else {
  56. // System.out.println("您的手机号" + phone + "是错误格式!!!");
  57. return false;
  58. }
  59. }
  60. public static boolean isEmail(String email) {
  61. String regex = "^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2,4})$";
  62. Pattern p = Pattern.compile(regex);
  63. Matcher m = p.matcher(email);
  64. boolean isMatch = m.matches();
  65. if (isMatch) {
  66. // System.out.println("您的手机号" + phone + "是正确格式@——@");
  67. return true;
  68. } else {
  69. // System.out.println("您的手机号" + phone + "是错误格式!!!");
  70. return false;
  71. }
  72. }
  73. }