校验工具类(手机电话号,身份证号,图片格式等)

41 篇文章 1 订阅
本文介绍了 ValidateUtils 类,提供了一系列用于校验手机号、电话号码格式、数字、图片类型、身份证号以及胜平负/胜负格式内容的方法。核心内容包括格式化手机号、正则表达式验证和基本数据验证功能。
摘要由CSDN通过智能技术生成
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 校验工具类
 */
public class ValidateUtils {
	
	/**
	 * 手机号格式化
	 */
	public static String formatMobile(String str) {   
        str = str.replace("+86", "");
        str = str.replace("-", "");
        str = str.trim();
        return str;  
    } 
	
	/** 
     * 手机号验证 
     */  
    public static boolean isMobile(String str) {
    	String string = ValidateUtils.formatMobile(str);
        Pattern p = null;  
        Matcher m = null;  
        boolean b = false;   
        p = Pattern.compile("^[1][0-9]{10}$"); // 验证手机号  
        m = p.matcher(string);  
        b = m.matches();   
        return b;  
    } 
    
    /** 
     * 电话号码验证 
     */  
    public static boolean isPhone(String str) {   
        Pattern p1 = null,p2 = null;  
        Matcher m = null;  
        boolean b = false;    
        p1 = Pattern.compile("^[0][1-9]{2,3}-[0-9]{5,10}$");  // 验证带区号的  
        p2 = Pattern.compile("^[1-9]{1}[0-9]{5,8}$");         // 验证没有区号的  
        if(str.length() >9)  
        {   m = p1.matcher(str);  
            b = m.matches();    
        }else{  
            m = p2.matcher(str);  
            b = m.matches();   
        }    
        return b;  
    } 
    
    /**
     * 验证数字
     */
    public static boolean isNumeric(String str){
		for (int i = 0; i < str.length(); i++) {
			if (!Character.isDigit(str.charAt(i))) {
				return false;
			}
		}
		return true;
    }
    
    /**
     * 是否图片格式
     */
    public static boolean isPhoto(String type){
    	if(StringUtils.isEmpty(type)){
    		return false;
        }
		String photoTypes = "jpg,jpeg,gif,png,bmp";
		if(photoTypes.contains(type)){
			return true;
		}
		return false;
    }

    /**
     * 身份证号是否合法
     */
    public static boolean isIDCard(String idCard){
    	String str = IDCardUtils.IDCardValidate(idCard);
    	if(BaseConst.SUCCESS.equals(str)){
    		return true;
    	}
    	return false;
    }
    
    /**
     * 检测是否胜平负格式的内容
     */
    public static boolean isSPFContent(String str){
    	String[] arr = str.split("#");
    	if(arr==null){
    		return false;
    	}
    	if(arr.length!=3){
    		return false;
    	}
    	for( String string:arr ){
    		if((!"1".equals(string))&&(!"0".equals(string))){
    			return false;
    		}
    	}
    	return true;
    }
    
    /**
     * 检测是否胜负格式的内容
     */
    public static boolean isSFContent(String str){
    	String[] arr = str.split("#");
    	if(arr==null){
    		return false;
    	}
    	if(arr.length!=2){
    		return false;
    	}
    	for( String string:arr ){
    		if((!"1".equals(string))&&(!"0".equals(string))){
    			return false;
    		}
    	}
    	return true;
    }
    
//    public static void main(String[] args) {
		System.out.println(formatMobile("+86 152-4707-8596"));
		System.out.println(isNumeric("233.2"));
//    	System.out.println(isSPFContent("1#1#0"));
//	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LC超人在良家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值