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"));
// }
}
校验工具类(手机电话号,身份证号,图片格式等)
最新推荐文章于 2023-12-26 09:26:33 发布
本文介绍了 ValidateUtils 类,提供了一系列用于校验手机号、电话号码格式、数字、图片类型、身份证号以及胜平负/胜负格式内容的方法。核心内容包括格式化手机号、正则表达式验证和基本数据验证功能。
摘要由CSDN通过智能技术生成