后台数据校验
2015-12-29 17:04
1196 查看
package validate;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
/**
* @author lkw
* @time 2015年11月30日
* @email lukw@eastcom-sw.com
*/
public class validateExcel {
// Pattern pattern = Pattern.compile(“[0-9]*”);
// return pattern.matcher(str).matches();
}
}
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
/**
* @author lkw
* @time 2015年11月30日
* @email lukw@eastcom-sw.com
*/
public class validateExcel {
public static String validateExcelFormat(String value ,Map<String,Object> rules,String type){ StringBuffer message = new StringBuffer(); String validate = isRequired(value,rules); getMessage(message, validate); if(StringUtils.isNotBlank(validate)) return message.toString(); if("TEXTFIELD".equals(type)){ validate = validateTextField(value,rules); getMessage(message, validate); }else if("TEXTAREA".equals(type)){ validate = validateTextArea(value,rules); getMessage(message, validate); }else if("DATETIME".equals(type)){ validate = validateDateTime(value); getMessage(message, validate); }else if("INTEGERFIELD".equals(type)){ validate = validateInteger(value); getMessage(message, validate); }else if("DOUBLEFIELD".equals(type)){ validate = validateDouble(value); getMessage(message, validate); }else if("PHONEFIELD".equals(type)){ validate = validatePhoneNo(value); getMessage(message, validate); }else if("EMAILFIELD".equals(type)){ validate = validateEmail(value); getMessage(message, validate); }else if("IP".equals(type)){ validate = validateIP(value); getMessage(message, validate); } return message.toString(); } private static String validateTextArea(String value,Map<String,Object> rules){ return null; } private static String validateTextField(String value,Map<String,Object> map){ StringBuffer message = new StringBuffer(); String validate = null; Object length = map.get("minLength"); if(length != null){ int minLength = Integer.valueOf(String.valueOf(map.get("minLength"))); validate = minLenght(value, minLength); getMessage(message, validate); } length = map.get("maxLength"); if(length != null){ int minLength = Integer.valueOf(String.valueOf(map.get("minLength"))); validate = maxLength(value, minLength); getMessage(message, validate); } return message.toString(); } private static String validateDateTime(String value) { // 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); // 设置lenient为false.否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01 dateFormat.setLenient(false); boolean flag = true; try { dateFormat.parse(value); } catch (Exception e) { flag = false; } if(!flag) return "请输入正确的日期"; return null; } private static String validateInteger(String value) { Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$"); if(!pattern.matcher(value).matches()) return "请输入正确的整数"; return null;
// Pattern pattern = Pattern.compile(“[0-9]*”);
// return pattern.matcher(str).matches();
}
private static String validateDouble(String value) { boolean flag = true; try { Double.parseDouble(value); } catch (NumberFormatException ex) { flag = false; } if(!flag) return "请输入正确的数字"; return null; } private static String maxLength(String value ,int length){ if(StringUtils.isNotBlank(value)&&value.length()>length) return "超过最大长度"; return null; } private static String minLenght(String value ,int length){ if(StringUtils.isNotBlank(value)&&value.length()<length) return "最小长度为" + length; return null; } private static String validatePhoneNo(String value) { String check = "^(13[4,5,6,7,8,9]|15[0,8,9,1,7]|188|187)\\d{8}$"; Pattern regex = Pattern.compile(check); Matcher matcher = regex.matcher(value); if(!matcher.matches()) return "请输入正确的手机号码 !! "; return null; } private static String validateEmail(String value) { String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"; Pattern regex = Pattern.compile(check); Matcher matcher = regex.matcher(value); if(!matcher.matches()) return "请输入正确的邮箱!! "; return null; } private static String isRequired(String value,Map<String,Object> rules){ Boolean flag = (Boolean)rules.get("isNull"); if(flag && StringUtils.isBlank(value)) return "为必填项"; else return null; } private static String validateIP(String value){ String ip ="\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b"; Pattern pattern = Pattern.compile(ip); Matcher matcher = pattern.matcher(value); if(!matcher.matches()) return "请输入正确的ip地址"; return null; } private static void getMessage(StringBuffer message, String validate) { if(StringUtils.isNotBlank(validate)) message.append(validate); } public static void main(String[] args) { Map<String,Object> rules = new HashMap<String, Object>(); rules.put("minLength", 5); rules.put("maxLength", 10); rules.put("isNull", false); String type = "IP"; String value ="192.168.1.1333"; String message = validateExcelFormat(value, rules, type); System.out.println("message = "+message); }
}
相关文章推荐
- 房产界已上市和IPO路上的难兄难弟:房天下与房多多
- 我是运营,我没有假期
- ElectronMail:ProtonMail 和 Tutanota 的桌面客户端
- 雷军是谁,他为什么而奋斗?小米上市和雷军的英雄主义
- IP网络路由技术
- 如何利用“IP地址欺骗”
- 新手看招:避免网络 IP 地址被非法修改
- 网络管理之IP地址篇
- ip地址基础知识
- DB2数据库的安装
- 脚本之家因服务器资源紧张 换了ip并升级了带宽
- C#实现把指定数据写入串口
- IP地址组成与类型
- “传奇”图象数据存储方式
- 修复mysql数据库
- 计算机名DNS网关IP(大家可以用来学习)
- Windows2003下批量添加和导出所有ip BAT脚本
- 一个能对访问者进行编号、记录访问次数、IP、时间的统计制作实例
- 浅析SQL数据操作语句