Java 通用的验证类

  1. 呵呵 今天还有一点时间整理了一下java的通过验证格式  以备以后使用
  2.   
  3. import java.util.regex.Matcher;  
  4. import java.util.regex.Pattern;  
  5.   
  6.    
  7. public class ValidateUtil 
  8. {  
  9.   
  10.     // 保存每个月的天数  
  11.     private static final int[] DAYS_OF_MONTH = { 31283130313031,  
  12.             3130313031 };  
  13.   
  14.     // 日历的起始年份  
  15.     public static final int START_YEAR = 1900;  
  16.   
  17.     // 日历的结束年份  
  18.     public static final int END_YEAR = 2100;  
  19.   
  20.     private static final String ZERO_STRING = "0";  
  21.   
  22.     /*************************************************************************** 
  23.      * 匹配英文字母 或者汉字 如"Shenzhen" "深圳" 
  24.      *  
  25.      * @param str 待匹配字符串 
  26.      * @return true 匹配通过 false 匹配失败 
  27.      */  
  28.     public static boolean isValidEnglishOrChinese(String str)  
  29.     {  
  30.         // 1、[A-Za-z]* 英文字母的匹配 一次或者多次  
  31.         // 2、[\u4E00-\u9FA5]* 汉字匹配 一次或者多次  
  32.         boolean flag = false;  
  33.         Pattern p = Pattern.compile("^[A-Za-z]*|[\u4E00-\u9FA5]*$");  
  34.         if (str != null)  
  35.         {  
  36.             Matcher match = p.matcher(str);  
  37.             flag = match.matches();  
  38.         }  
  39.         return flag;  
  40.     }  
  41.   
  42.     /*************************************************************************** 
  43.      * 匹配英中文姓名 与英文名 英文名格式为:姓与名之间用/隔开 例如Green/Jim King 
  44.      *  
  45.      * @param str 待匹配字符串 
  46.      * @return true 匹配通过 false 匹配失败 
  47.      */  
  48.     public static boolean isValidName(String str)  
  49.     {  
  50.         // 1、[A-Za-z]* 英文字母的匹配 一次或者多次  
  51.         // 2、[\u4E00-\u9FA5]* 汉字匹配 一次或者多次  
  52.         boolean flag = false;  
  53.         Pattern p = Pattern  
  54.                 .compile("^([A-Za-z]+[\\/][A-Za-z]+)|[\u4E00-\u9FA5]*");  
  55.         if (str != null)  
  56.         {  
  57.             Matcher match = p.matcher(str);  
  58.             flag = match.matches();  
  59.         }  
  60.         return flag;  
  61.     }  
  62.   
  63.     /*************************************************************************** 
  64.      * 验证身份证号码 15位 18位 
  65.      *  
  66.      * @param cardStr 身份证字符串 
  67.      * @return true 合法 false 不合法 
  68.      */  
  69.     public static boolean isValidIdCard(String cardStr)  
  70.     {  
  71.         boolean flag = false;  
  72.         Pattern pEighteen = Pattern.compile("^\\d{17}(\\d{1}|x)$");// 18位身份证号码  
  73.         // 包括末尾是“x”的校验码  
  74.         Pattern pFifteen = Pattern.compile("^\\d{15}$");// 15位身份证号码  
  75.         if (cardStr != null)  
  76.         {  
  77.             if (pEighteen.matcher(cardStr).matches()) // 18位身份证号码验证通过  
  78.             {  
  79.                 if (isValidDate(cardStr.substring(614)))// 18位身份证号码  
  80.                 // 出生年月日验证通过  
  81.                 {  
  82.                     flag = true;  
  83.                 }  
  84.             }  
  85.             if (pFifteen.matcher(cardStr).matches()) // 15位身份证号码验证通过  
  86.             {  
  87.                 if (isValidDay(cardStr.substring(612))) // 15位身份证出身年月日的验证  
  88.                 {  
  89.                     flag = true;  
  90.                 }  
  91.             }  
  92.         }  
  93.         return flag;  
  94.   
  95.     }  
  96.   
  97.     /*************************************************************************** 
  98.      * 正整数验证 
  99.      *  
  100.      * @param str 待验证字符串 
  101.      * @return true 验证通过 false 验证失败 
  102.      */  
  103.     public static boolean isValidInteger(String str)  
  104.     {  
  105.         boolean flag = false;  
  106.         Pattern p = Pattern.compile("^\\d*$");  
  107.         if (str != null)  
  108.         {  
  109.             Matcher match = p.matcher(str);  
  110.             flag = match.matches();  
  111.         }  
  112.         return flag;  
  113.     }  
  114.   
  115.     /*************************************************************************** 
  116.      * 整数验证(包括正整数与 负整数) 
  117.      *  
  118.      * @param str 待验证字符串 
  119.      * @return true 验证通过 false 验证失败 
  120.      */  
  121.     public static boolean isValidNo(String str)  
  122.     {  
  123.         boolean flag = false;  
  124.         Pattern p = Pattern.compile("^-?\\d*$");  
  125.         if (str != null)  
  126.         {  
  127.             Matcher match = p.matcher(str);  
  128.             flag = match.matches();  
  129.         }  
  130.         return flag;  
  131.     }  
  132.   
  133.     /** 
  134.      *  验证非负整数(正整数+0) 
  135.      *  
  136.      * @param str 待验证字符串 
  137.      * @return true 验证通过 false 验证失败 
  138.      */  
  139.     public static boolean isValidNonNegative(String str)  
  140.     {  
  141.         boolean flag = false;  
  142.         Pattern p = Pattern.compile("^\\d+$");  
  143.         if (str != null)  
  144.         {  
  145.             Matcher match = p.matcher(str);  
  146.             flag = match.matches();  
  147.         }  
  148.         return flag;  
  149.     }  
  150.   
  151.     /** 
  152.      * 验证非负整数(正整数+0) 
  153.      *  
  154.      * @param str 待验证字符串 
  155.      * @return true 验证通过 false 验证失败 
  156.      */  
  157.     public static boolean isValidPositiveInteger(String str)  
  158.     {  
  159.         boolean flag = false;  
  160.         Pattern p = Pattern.compile("^\\d+$");  
  161.         if (str != null)  
  162.         {  
  163.             Matcher match = p.matcher(str);  
  164.             flag = match.matches();  
  165.             if(ZERO_STRING.equals(str))  
  166.             {  
  167.                 flag = false;  
  168.             }     
  169.         }  
  170.           
  171.         return flag;  
  172.     }  
  173.   
  174.     /*************************************************************************** 
  175.      * 匹配英文字母(汉语拼音) 
  176.      *  
  177.      * @param str 待匹配字符串 
  178.      * @return true 匹配通过 false 匹配失败 
  179.      */  
  180.     public static boolean isValidEnglish(String str)  
  181.     {  
  182.         boolean flag = false;  
  183.         Pattern p = Pattern.compile("^[A-Za-z]*$");  
  184.         if (str != null)  
  185.         {  
  186.             Matcher match = p.matcher(str);  
  187.             flag = match.matches();  
  188.         }  
  189.         return flag;  
  190.     }  
  191.   
  192.     /*************************************************************************** 
  193.      * 匹配英文字母 或者汉字,数字 过滤特殊字符 
  194.      *  
  195.      * @param str 待匹配字符串 
  196.      * @return true 匹配通过 false 匹配失败 
  197.      */  
  198.     public static boolean isValidNonSpecialChar(String str)  
  199.     {  
  200.         boolean flag = false;  
  201.         Pattern p = Pattern.compile("^[A-Za-z\u4E00-\u9FA5\\d]*$");  
  202.         if (str != null)  
  203.         {  
  204.             Matcher match = p.matcher(str);  
  205.             flag = match.matches();  
  206.         }  
  207.         return flag;  
  208.     }  
  209.   
  210.     /** 
  211.      * 验证HH时间格式的时间范围是否大于等于三小时 **注意此方法必须在isValidHour格式验证通过后调用 
  212.      *  
  213.      * @param startHour 开始时间 HH 
  214.      * @param endHour 结束时间HH 
  215.      * @return true 通过 false 不通过 
  216.      */  
  217.     public static boolean isVaildHourZone(String startHour, String endHour)  
  218.     {  
  219.         boolean flag = false;  
  220.         if (startHour != null && endHour != null)  
  221.         {  
  222.             if (isValidHour(startHour) && isValidHour(endHour)) // 格式验证,避免可能抛类型转换异常  
  223.             {  
  224.                 int sHour = Integer.parseInt(startHour);  
  225.                 int eHour = Integer.parseInt(endHour);  
  226.                 flag = (eHour - sHour >= 3);  
  227.             }  
  228.         }  
  229.         return flag;  
  230.   
  231.     }  
  232.   
  233.     /*************************************************************************** 
  234.      * 验证结束时间是否大于开始时间 **注意:此方法必须先调用isValidDate 方法后调用 yyMMdd 
  235.      *  
  236.      * @param startDate 开始时间 
  237.      * @param endDate 结束时间 
  238.      * @return true 验证通过 false 验证失败 
  239.      */  
  240.     public static boolean isValidTimeZone(String startDate, String endDate)  
  241.     {  
  242.         boolean flag = false;  
  243.         if (startDate != null && endDate != null)  
  244.         {  
  245.             if (isValidDate(startDate) && isValidDate(endDate)) // 格式验证,避免可能抛类型转换异常  
  246.             {  
  247.                 flag = (Integer.parseInt(endDate) > Integer.parseInt(startDate));  
  248.             }  
  249.         }  
  250.         return flag;  
  251.     }  
  252.   
  253.     /*************************************************************************** 
  254.      * 验证结束时间是否大于等于开始时间 **注意:此方法必须先调用isValidDate 方法后调用 yyMMdd(包含等于) 
  255.      *  
  256.      * @param startDate 开始时间 
  257.      * @param endDate 结束时间 
  258.      * @return true 验证通过 false 验证失败 
  259.      */  
  260.     public static boolean isValidTwoTimes(String startDate, String endDate)  
  261.     {  
  262.         boolean flag = false;  
  263.         if (startDate != null && endDate != null)  
  264.         {  
  265.             if (isValidDate(startDate) && isValidDate(endDate)) // 格式验证,避免可能抛类型转换异常  
  266.             {  
  267.                 flag = (Integer.parseInt(endDate) > Integer.parseInt(startDate) || Integer  
  268.                         .parseInt(endDate) == Integer.parseInt(startDate));  
  269.             }  
  270.         }  
  271.         return flag;  
  272.     }  
  273.   
  274.     /*************************************************************************** 
  275.      * 验证电话号码 后可接分机号 区号3位或者4位 电话7位或者8位后 后面可加3位或者4位分机号 
  276.      *  
  277.      * @param telephoeNo 电话号码字符串 
  278.      * @return 
  279.      */  
  280.     public static boolean isValidTelephoeNo(String telephoeNo)  
  281.     {  
  282.         // 1、\\d{3,4} 区号 3位或者4位的匹配  
  283.         // 2、\\d{7,8} 号码 7味或者8位的匹配  
  284.         // 3、(\\d{3,4})? 分机号3位或者4位的匹配 ?可匹配一次或者两次  
  285.         boolean flag = false;  
  286.         Pattern p = Pattern.compile("^\\d{3,4}\\d{7,8}(\\d{3,4})?$");  
  287.         Matcher match = p.matcher(telephoeNo);  
  288.         if (telephoeNo != null)  
  289.         {  
  290.             flag = match.matches();  
  291.         }  
  292.         return flag;  
  293.     }  
  294.   
  295.     /*************************************************************************** 
  296.      * 验证手机号码 
  297.      *  
  298.      * @param telNo 电话号码字符串 130到139 和 150,152 ,157,158,159 ,186,189,187 
  299.      * @return 
  300.      */  
  301.     public static boolean isValidMobileNo(String mobileNo)  
  302.     {  
  303.         // 1、(13[0-9])|(15[02789])|(18[679]) 13段 或者15段 18段的匹配  
  304.         // 2、\\d{8} 整数出现8次  
  305.         boolean flag = false;  
  306.         // Pattern p = Pattern.compile("^(1[358][13567890])(\\d{8})$");  
  307.         Pattern p = Pattern  
  308.                 .compile("^((13[0-9])|(15[02789])|(18[679]))\\d{8}$");  
  309.         Matcher match = p.matcher(mobileNo);  
  310.         if (mobileNo != null)  
  311.         {  
  312.             flag = match.matches();  
  313.         }  
  314.         return flag;  
  315.     }  
  316.   
  317.     /*************************************************************************** 
  318.      * 验证是否是正确的邮箱格式 
  319.      *  
  320.      * @param email 
  321.      * @return true表示是正确的邮箱格式,false表示不是正确邮箱格式 
  322.      */  
  323.     public static boolean isValidEmail(String email)  
  324.     {  
  325.         // 1、\\w+表示@之前至少要输入一个匹配字母或数字或下划线 \\w 单词字符:[a-zA-Z_0-9]  
  326.         // 2、(\\w+\\.)表示域名. 如新浪邮箱域名是sina.com.cn  
  327.         // {1,3}表示可以出现一次或两次或者三次.  
  328.         String reg = "\\w+@(\\w+\\.){1,3}\\w+";  
  329.         Pattern pattern = Pattern.compile(reg);  
  330.         boolean flag = false;  
  331.         if (email != null)  
  332.         {  
  333.             Matcher matcher = pattern.matcher(email);  
  334.             flag = matcher.matches();  
  335.         }  
  336.         return flag;  
  337.     }  
  338.   
  339.     /*************************************************************************** 
  340.      * 验证整点时间格式是否正确 HH格式 时间范围00时~23时 
  341.      *  
  342.      * @param hour 时间格式字符串 
  343.      * @return true表示是正确的整点时间格式,false表示不是正确整点时间格式 
  344.      */  
  345.     public static boolean isValidHour(String hour)  
  346.     {  
  347.         boolean flag = false;  
  348.         String reg = "^[0-2][0-9]$";  
  349.         Pattern pattern = Pattern.compile(reg);  
  350.         if (hour != null)  
  351.         {  
  352.             Matcher matcher = pattern.matcher(hour);  
  353.             flag = matcher.matches();  
  354.             int firstNum = Integer.parseInt(hour.substring(01));  
  355.             if (flag && firstNum == 2)  
  356.             {  
  357.                 int secondNum = Integer.parseInt(hour.substring(12));  
  358.                 flag = secondNum < 4// 时间小于24时  
  359.             }  
  360.         }  
  361.         return flag;  
  362.     }  
  363.   
  364.     /*************************************************************************** 
  365.      * 匹配日期格式 yyMMdd 并验证日期是否合法 此方法忽略了闰年的验证 用于15位身份证出生日期的验证 
  366.      *  
  367.      * @param dayStr 日期字符串 
  368.      * @return true 日期合法 false 日期非法 
  369.      */  
  370.     public static boolean isValidDay(String dayStr)  
  371.     {  
  372.         Pattern p = Pattern.compile("^\\d{2}\\d{2}\\d{2}$");  
  373.         Matcher match = p.matcher(dayStr);  
  374.         if (dayStr != null)  
  375.         {  
  376.             if (match.matches()) // 格式验证通过 yyMMdd  
  377.             {  
  378.                 int month = Integer.parseInt(dayStr.substring(24)); // 月  
  379.                 int day = Integer.parseInt(dayStr.substring(46)); // 日  
  380.                 if (!isValidMonth(month))  
  381.                 {  
  382.                     return false// 月份不合法  
  383.                 }  
  384.                 if (!(day >= 1 && day <= DAYS_OF_MONTH[month - 1]))  
  385.                 {  
  386.                     return false// 日期不合法  
  387.                 }  
  388.                 return true;  
  389.             }  
  390.   
  391.             return false;  
  392.         }  
  393.         else  
  394.         {  
  395.             return false;  
  396.         }  
  397.     }  
  398.   
  399.     /*************************************************************************** 
  400.      * 匹配日期格式 yyyyMMdd 并验证日期是否合法 
  401.      *  
  402.      * @param date 日期字符串 
  403.      * @return true 日期合法 false 日期非法 
  404.      */  
  405.     public static boolean isValidDate(String date)  
  406.     {  
  407.         // 1、 \\d{4} 年,\\d{2}月,\\d{2}日匹配  
  408.         Pattern p = Pattern.compile("^\\d{4}\\d{2}\\d{2}$");  
  409.         Matcher match = p.matcher(date);  
  410.         if (date != null)  
  411.         {  
  412.             if (match.matches()) // 格式验证通过 yyyyMMdd  
  413.             {  
  414.                 int year = Integer.parseInt(date.substring(04)); // 年  
  415.                 int month = Integer.parseInt(date.substring(46)); // 月  
  416.                 int day = Integer.parseInt(date.substring(68)); // 日  
  417.                 if (!isValidYear((year)))  
  418.                 {  
  419.                     return false// 年份不在有效年份中  
  420.                 }  
  421.                 if (!isValidMonth(month))  
  422.                 {  
  423.                     return false// 月份不合法  
  424.                 }  
  425.                 if (!isValidDay(year, month, day))  
  426.                 {  
  427.                     return false// 日期不合法  
  428.                 }  
  429.                 return true;  
  430.             }  
  431.   
  432.             return false;  
  433.         }  
  434.         else  
  435.         {  
  436.             return false;  
  437.         }  
  438.         // return Pattern.matches("", date);  
  439.     }  
  440.   
  441.     /** 
  442.      * 检查year是否在有效的年份范围内 此处验证大于1900年 小于2101年 
  443.      *  
  444.      * @param year 
  445.      * @return 
  446.      */  
  447.     public static boolean isValidYear(int year)  
  448.     {  
  449.         return year >= START_YEAR && year <= END_YEAR;  
  450.     }  
  451.   
  452.     /** 
  453.      * 验证月份是否在有效月份内 
  454.      *  
  455.      * @param month 
  456.      * @return 
  457.      */  
  458.     public static boolean isValidMonth(int month)  
  459.     {  
  460.         return month >= 1 && month <= 12;  
  461.     }  
  462.   
  463.     /** 
  464.      * 检查天数是否在有效的范围内,因为天数会根据年份和月份的不同而不同 所以必须依赖年份和月份进行检查 
  465.      *  
  466.      * @param year 
  467.      * @param month 
  468.      * @param day 
  469.      * @return 
  470.      */  
  471.     public static boolean isValidDay(int year, int month, int day)  
  472.     {  
  473.         if (month == 2 && isLeapYear(year))// 闰年且是2月份  
  474.         {  
  475.             return day >= 1 && day <= 29;  
  476.         }  
  477.         return day >= 1 && day <= DAYS_OF_MONTH[month - 1];// 其他月份  
  478.     }  
  479.   
  480.     /** 
  481.      * 验证是否是闰年 
  482.      *  
  483.      * @param year 
  484.      * @return 
  485.      */  
  486.     public static boolean isLeapYear(int year)  
  487.     {  
  488.         return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;  
  489.     }  
  490.   
  491.     /** 
  492.      * 验证用户名注册是否合法-----------由数字、26个英文字母或者下划线组成的字符串 
  493.      *  
  494.      * @param userName 
  495.      * @return 
  496.      */  
  497.     public static boolean isRegUserName(String userName)  
  498.     {  
  499.   
  500.         String str = "^\\w+$";  
  501.         boolean flag = true;  
  502.         if (userName != null)  
  503.         {  
  504.             Pattern p = Pattern.compile(str);  
  505.             Matcher match = p.matcher(userName);  
  506.             flag = match.matches();  
  507.         }  
  508.         return flag;  
  509.     } 

  510. /**
  511.  * 验证IP地址
  512.  *
  513. * @param 待验证的字符串
  514.  * @return 如果是符合格式的字符串,返回 true ,否则为 false
  515.   */
  516.   public static boolean isIP(String str) {
  517.          String num = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";
  518.          String regex = "^" + num + "\\." + num + "\\." + num + "\\." + num
  519.             + "$";
  520.           return match(regex, str);
  521.  }

  522. /**
  523.      * 验证网址Url
  524.      * 
  525.      * @param 待验证的字符串
  526.      * @return 如果是符合格式的字符串,返回 true ,否则为 false
  527.      */
  528.     public static boolean IsUrl(String str) {
  529.         String regex = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
  530.         return match(regex, str);
  531.     }

  532. /**
  533.      * 验证输入密码条件(字符与数据同时出现)
  534.      * 
  535.      * @param 待验证的字符串
  536.      * @return 如果是符合格式的字符串,返回 true ,否则为 false
  537.      */
  538.     public static boolean IsPassword(String str) {
  539.         String regex = "[A-Za-z]+[0-9]";
  540.         return match(regex, str);
  541.     }

  542. /**
  543.      * 验证输入密码长度 (6-18位)
  544.      * 
  545.      * @param 待验证的字符串
  546.      * @return 如果是符合格式的字符串,返回 true ,否则为 false
  547.      */
  548.     public static boolean IsPasswLength(String str) {
  549.         String regex = "^\\d{6,18}$";
  550.         return match(regex, str);
  551.     }

  552. /**
  553.      * 验证输入两位小数
  554.      * 
  555.      * @param 待验证的字符串
  556.      * @return 如果是符合格式的字符串,返回 true ,否则为 false
  557.      */
  558.     public static boolean IsDecimal(String str) {
  559.         String regex = "^[0-9]+(.[0-9]{2})?$";
  560.         return match(regex, str);
  561.     }

  562. /**
  563.      * 验证大写字母
  564.      * 
  565.      * @param 待验证的字符串
  566.      * @return 如果是符合格式的字符串,返回 true ,否则为 false
  567.      */
  568.     public static boolean IsUpChar(String str) {
  569.         String regex = "^[A-Z]+$";
  570.         return match(regex, str);
  571.     }

  572.     /**
  573.      * 验证小写字母
  574.      * 
  575.      * @param 待验证的字符串
  576.      * @return 如果是符合格式的字符串,返回 true ,否则为 false
  577.      */
  578.     public static boolean IsLowChar(String str) {
  579.         String regex = "^[a-z]+$";
  580.         return match(regex, str);
  581.     }
  582. }    
  583. 呵呵   下班了.................................................................! 

你可能感兴趣的:(JAVA编程)