java 验证 校验 的一些小方法

package com.cta.quality.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CheckUtil {

   
    /*
     *
     * 校验是否为空
     */
    public static boolean isEmpty(String str) {
       
        if(str == null || "".equals(str)) {
           
            return true;
           
        }
       
        return false;
       
    }
   
    /*
     *
     * 校验邮编
     */
    public static boolean checkPostCode(String postCode) {
       
         if(postCode.matches("[1-9]\\d{5}(?!\\d)")) { 
             
               return true;  
         } 
                        
         return false;  
                                       
    }
   
    /*
     *
     * 校验邮箱
     */
    public static boolean checkEmail(String email) {
       
         if(email.matches("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*")) {
             
             return true;  
                     
         }
                        
         return false;  
                                       
    }
   
    /*
     *
     * 校验是否为数字
     */
    public static boolean checkNumber(String number) {
       
        try{
           
            Double.valueOf(number);
       
        }
       
        catch(NumberFormatException e) {
           
            return false;
           
        }
             
        return true;   
                                       
    }
   
    /*
     *
     * 校验是否为整形
     */
    public static boolean isInt(String count) {
       
        try{
           
            Integer.valueOf(count);
       
        }
       
        catch(NumberFormatException e) {
           
            return false;
           
        }
             
        return true;   
                                       
    }
   
    /*
     *
     * 校验手机号码
     */
    public static boolean checkMobile(String tel) {
       
        return tel.matches("1[358][0-9]{9}");   
                                       
    }
   
    /*
     *
     * 校验电话
     */
    public static boolean checkPhone(String phone) {
       
        return phone.matches("[0-9]{3}-[0-9]{5,9}");   
                                       
    }  
   
    /*
     *
     * 校验网址
     */
    public static boolean checkWebSite(String webSite) {
       
        return webSite.matches("http://(([a-zA-z0-9]|-){1,}\\.){1,}[a-zA-z0-9]{1,}-*");   
                                       
    }
   
    /*
     *
     * 校验日期
     */
    public static boolean checkDate(String dateStr) {
       
        String eL = "^((\\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]))))))";
       
        Pattern p = Pattern.compile(eL);
        Matcher m = p.matcher(dateStr);
       
        if(!m.matches()) {
           
            return false;
           
        }
       
        return true;

    }

   
    public static void main(String[] args) {
       
        System.out.println(checkDate("2011-2-28"));
       
    }
   
   
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值