1.身份证校验
public class IDCardUtil {
final static Map<Integer, String> ZONE_NUM = new HashMap<Integer, String>();
static {
ZONE_NUM.put(11, "北京");
ZONE_NUM.put(12, "天津");
ZONE_NUM.put(13, "河北");
ZONE_NUM.put(14, "山西");
ZONE_NUM.put(15, "内蒙古");
ZONE_NUM.put(21, "辽宁");
ZONE_NUM.put(22, "吉林");
ZONE_NUM.put(23, "黑龙江");
ZONE_NUM.put(31, "上海");
ZONE_NUM.put(32, "江苏");
ZONE_NUM.put(33, "浙江");
ZONE_NUM.put(34, "安徽");
ZONE_NUM.put(35, "福建");
ZONE_NUM.put(36, "江西");
ZONE_NUM.put(37, "山东");
ZONE_NUM.put(41, "河南");
ZONE_NUM.put(42, "湖北");
ZONE_NUM.put(43, "湖南");
ZONE_NUM.put(44, "广东");
ZONE_NUM.put(45, "广西");
ZONE_NUM.put(46, "海南");
ZONE_NUM.put(50, "重庆");
ZONE_NUM.put(51, "四川");
ZONE_NUM.put(52, "贵州");
ZONE_NUM.put(53, "云南");
ZONE_NUM.put(54, "西藏");
ZONE_NUM.put(61, "陕西");
ZONE_NUM.put(62, "甘肃");
ZONE_NUM.put(63, "青海");
ZONE_NUM.put(64, "新疆");
ZONE_NUM.put(71, "台湾");
ZONE_NUM.put(81, "香港");
ZONE_NUM.put(82, "澳门");
ZONE_NUM.put(91, "外国");
}
final static int[] PARITYBIT = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
final static int[] POWER_LIST = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10,
5, 8, 4, 2};
public static boolean isIDCard(String certNo) {
if (certNo == null || (certNo.length() != 15 && certNo.length() != 18)) {
return false;
}
final char[] cs = certNo.toUpperCase().toCharArray();
int power = 0;
for (int i = 0; i < cs.length; i++) {
if (i == cs.length - 1 && cs[i] == 'X') {
break;
}
if (cs[i] < '0' || cs[i] > '9') {
return false;
}
if (i < cs.length - 1) {
power += (cs[i] - '0') * POWER_LIST[i];
}
}
if (!ZONE_NUM.containsKey(Integer.valueOf(certNo.substring(0, 2)))) {
return false;
}
String year = certNo.length() == 15 ? getIdcardCalendar() + certNo.substring(6, 8) : certNo.substring(6, 10);
final int iyear = Integer.parseInt(year);
if (iyear < 1900 || iyear > Calendar.getInstance().get(Calendar.YEAR)) {
return false;
}
String month = certNo.length() == 15 ? certNo.substring(8, 10) : certNo.substring(10, 12);
final int imonth = Integer.parseInt(month);
if (imonth < 1 || imonth > 12) {
return false;
}
String day = certNo.length() == 15 ? certNo.substring(10, 12) : certNo.substring(12, 14);
final int iday = Integer.parseInt(day);
if (iday < 1 || iday > 31) {
return false;
}
if (certNo.length() == 15) {
return true;
}
return cs[cs.length - 1] == PARITYBIT[power % 11];
}
private static int getIdcardCalendar() {
GregorianCalendar curDay = new GregorianCalendar();
int curYear = curDay.get(Calendar.YEAR);
int year2bit = Integer.parseInt(String.valueOf(curYear).substring(2));
return year2bit;
}
}
2.正则表达式匹配
public static boolean matcher(Object value, String eL) {
Pattern p = Pattern.compile(eL);
Matcher m = p.matcher(String.valueOf(value));
boolean b = m.matches();
return b;
}
3.手机号校验
public static boolean isMobile(Object value) {
if (isEmpty(value)) {
return false;
}
String eL = "^((13[0-9])|(14[0-9])|(15[^4,\\D])|(16[0-9])|(17[0-9])|(18[0-9])|(19[0-9]))\\d{8}$";
boolean b = matcher(value, eL);
return b;
}
4.邮箱地址校验
public static boolean isEmail(Object value) {
if (isEmpty(value)) {
return true;
}
String eL = "^([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)*@([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)+[\\.][A-Za-z]{2,3}([\\.][A-Za-z]{2})?$";
boolean b = matcher(value, eL);
return b;
}
5.获取本地ip
public static String getRemortIP(HttpServletRequest request) {
String ipAddress = request.getHeader("x-forwarded-for");
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
if ("127.0.0.1".equals(ipAddress) || "0:0:0:0:0:0:0:1".equals(ipAddress)) {
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
ipAddress = inet.getHostAddress();
} catch (Exception e) {
e.printStackTrace();
}
}
}
if (ipAddress != null && ipAddress.length() > 15) {
if (ipAddress.indexOf(SystemConfig.FXSHOP_SPLIT) > 0) {
ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
}
}
return ipAddress;
}