首页 > 技术文章 > java验证

yangmingfeng 2016-04-16 21:03 原文

[1].[代码] [Java]代码 跳至 [1]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package com.cn.wangk.util;
 
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
 
// 网站域名 联系电话 手机号码 邮政编码 邮箱
public class Validation {
    private static Logger logger = Logger.getLogger(Validation.class);
    /**
     * 正则验证方法
     *
     * @param regexstr
     * @param str
     * @return
     */
    public static boolean Match(String regexstr, String str) {
        Pattern regex = Pattern.compile(regexstr,Pattern.CASE_INSENSITIVE|Pattern.DOTALL);
        Matcher matcher = regex.matcher(str);
        return matcher.matches();
    }
 
    /**
     * 邮箱验证
     *
     * @param mail
     * @return
     */
    public static boolean MatchMail(String mail) {
        String mailregex = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        return Match(mailregex, mail);
    }
 
    /**
     * 手机验证
     *
     * @param mobile
     * @return
     */
    public static boolean MatchMobile(String mobile) {
        String mobileregex = "^(13[4,5,6,7,8,9]|15[0,8,9,1,7]|188|187)\\d{8}$";
        return Match(mobileregex, mobile);
    }
 
    /**
     * 电话验证
     *
     * @param Tel
     * @return
     */
    public static boolean MatchTel(String Tel) {
        String telregex = "(^[0-9]{3,4}-[0-9]{7,8}-[0-9]{3,4}$)|(^[0-9]{3,4}-[0-9]{7,8}$)|(^[0-9]{7,8}-[0-9]{3,4}$)|(^[0-9]{7,15}$)";
        return Match(telregex, Tel);
    }
 
    public static boolean Webdomain(String webdomain) {
        String webdomainregex = "http://([^/]+)/*";
        return Match(webdomainregex, webdomain);
    }
 
    public static boolean ZipCode(String zipcode) {
        String zipcoderegex = "^[0-9]{6}$";
        return Match(zipcoderegex, zipcode);
    }
 
    public static boolean IdCardNo(String idcard) {
        HashMap<Integer, String> area = new HashMap<Integer, String>();
        area.put(11, "北京");
        area.put(12, "天津");
        area.put(13, "河北");
        area.put(14, "山西");
        area.put(15, "内蒙古");
        area.put(21, "辽宁");
        area.put(22, "吉林");
        area.put(23, "黑龙江");
        area.put(31, "上海");
        area.put(32, "江苏");
        area.put(33, "浙江");
        area.put(34, "安徽");
        area.put(35, "福建");
        area.put(36, "江西");
        area.put(37, "山东");
        area.put(41, "河南");
        area.put(42, "湖北");
        area.put(43, "湖南");
        area.put(44, "广东");
        area.put(45, "广西");
        area.put(46, "海南");
        area.put(50, "重庆");
        area.put(51, "四川");
        area.put(52, "贵州");
        area.put(53, "云南");
        area.put(54, "西藏");
        area.put(61, "陕西");
        area.put(62, "甘肃");
        area.put(63, "青海");
        area.put(64, "宁夏");
        area.put(65, "新疆");
        area.put(71, "台湾");
        area.put(81, "香港");
        area.put(82, "澳门");
        area.put(91, "国外");
        if(StringUtils.isBlank(idcard)) return false;
        logger.info("身份证所在地:"+area.get(Integer.parseInt(idcard.substring(0, 2))));
        if(area.get(Integer.parseInt(idcard.substring(0, 2)))==null) return false;
        if(!(idcard.length()==15||idcard.length()==18)) return false;
        if(idcard.length()==15){
            //老身份证
            int year = Integer.parseInt(idcard.substring(2,6))+1900;
            String ereg;
            if (year % 4 == 0||(year% 100 == 0 &&year % 4 == 0 )){
                ereg="^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$";//测试出生日期的合法性
             }else{
                 ereg="^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$";//测试出生日期的合法性
             }
             if(Match(ereg, idcard))
                 return true;
             else
                 return false;
              
        }else if(idcard.length()==18){
            //新省份证
             //18位身份号码检测
             //出生日期的合法性检查
             //闰年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))
             //平年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))
            int year = Integer.parseInt(idcard.substring(2,6))+1900;
            String ereg;
             if (year % 4 == 0 ||(year % 100 == 0 && year%4 == 0 )){
                 ereg="^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$";//闰年出生日期的合法性正则表达式
             }else{
                 ereg="^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$";//平年出生日期的合法性正则表达式
             }
             if(Match(ereg, idcard)){//测试出生日期的合法性
             //计算校验位
                 int[] idcards = new int[18];
                 for (int i = 0; i < idcard.length(); i++) {
                    idcards[i]=Integer.parseInt(idcard.substring(i, i+1));
                }
                 int S = (idcards[0] + idcards[10]) * 7
                 + (idcards[1] + idcards[11]) * 9
                 + (idcards[2] + idcards[12]) * 10
                 + (idcards[3] + idcards[13]) * 5
                 + (idcards[4] + idcards[14]) * 8
                 + (idcards[5] + idcards[15]) * 4
                 + (idcards[6] + idcards[16]) * 2
                 + idcards[7] * 1
                 + idcards[8] * 6
                 + idcards[9] * 3 ;
                 int Y = S % 11;
                 String M = "F";
                 String JYM = "10X98765432";
                 M = JYM.substring(Y,Y+1);//判断校验位
                 if(StringUtils.equalsIgnoreCase(M, String.valueOf(idcards[17])))
                         return true; //检测ID的校验位
                 else
                     return false;
             }
             else
                 return false;
        }
        return false;
    }
 
    public static void main(String[] args) {
        // 电子邮件
//      System.out.println(MatchMail("dffdfdf@qq.com"));
//      System.out.println(MatchMobile("13555655606"));
        //网上摘的几个身份证
        System.out.println(IdCardNo("420101198001300053"));
        System.out.println(IdCardNo("430911800709422"));
        System.out.println(IdCardNo("430903198007094228"));
 
    }
 
}

推荐阅读