利用JavaWeb,简单实现身份ID校验系统

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Welcome CheckID System</title>
    <style type="text/css">
        .st1{
            color:blue;
        }
        .st2{
            color: orangered;
        }
        .st3{
            color: magenta;
        }
    </style>
</head>
<body>
<div align="center">
<form action="" methed="get">
    <h1 class="st1">请输入您的身份证号:</h1>
    <br>
    <h2 class="st2">Please enter your ID number</h2>
    <br>
    <h4 class="st3">注:我们会运用<a href="https://www.98158.com/bjhuzheng/shenfenzheng/737.html"> 国家身份证ID算法</a>对您的身份ID进行核查</h4>
    <h5 class="st3">详情参考-><a href="http://www.npc.gov.cn/npc/c12435/201110/6a4ca1c55fbe4ef58927ea6180305b15.shtml ">《中华人民共和国居民身份证法》</a></h5>
        <br>
        <input id="id" name="id" type="text" style="width: 200px; height: 75px;" size="10" required onchange="this.form.submit();"/>
    <br><br><br><br>
<%
    request.setCharacterEncoding("UTF-8");//识别汉字等
    String ID = request.getParameter("id");//输入
    if(ID!=null) {
        int flag = 0;
        int[] c= new int[18];
        if(ID.length()==18){
        String a1 = ID.substring(0,1);
        int b1;
        b1 = a1.charAt(0)-48;
        if(b1>9)
        {
            flag=1;
            c[1]=1;
        }
        String a2 = ID.substring(1,2);
            int b2;
            b2 = a2.charAt(0)-48;
            if(b2>9)
            {
                flag=1;
                c[2]=2;
            }
        String a3 = ID.substring(2,3);
            int b3;
            b3 = a3.charAt(0)-48;
            if(b3>9)
            {
                flag=1;
                c[3]=3;
            }
        String a4 = ID.substring(3,4);
            int b4;
            b4 = a4.charAt(0)-48;
            if(b4>9)
            {
                flag=1;
                c[4]=4;
            }
        String a5 = ID.substring(4,5);
            int b5;
            b5 = a5.charAt(0)-48;
            if(b5>9)
            {
                flag=1;
                c[5]=5;
            }
        String a6 = ID.substring(5,6);
            int b6;
            b6 = a6.charAt(0)-48;
            if(b6>9)
            {
                flag=1;
                c[6]=6;
            }
        String a7 = ID.substring(6,7);
            int b7;
            b7 = a7.charAt(0)-48;
            if(b7>9)
            {
                flag=1;
                c[7]=7;
            }
        String a8 = ID.substring(7,8);
            int b8;
            b8 = a8.charAt(0)-48;
            if(b8>9)
            {
                flag=1;
                c[8]=8;
            }
        String a9 = ID.substring(8,9);
            int b9;
            b9 = a9.charAt(0)-48;
            if(b9>9)
            {
                flag=1;
                c[9]=9;
            }
        String a10 = ID.substring(9,10);
            int b10;
            b10 = a10.charAt(0)-48;
            if(b10>9)
            {
                flag=1;
                c[10]=10;
            }
        String a11 = ID.substring(10,11);
            int b11;
            b11 = a11.charAt(0)-48;
            if(b11>9)
            {
                flag=1;
                c[11]=11;
            }
        String a12 = ID.substring(11,12);
            int b12;
            b12 = a12.charAt(0)-48;
            if(b12>9)
            {
                flag=1;
                c[12]=12;
            }
        String a13 = ID.substring(12,13);
            int b13;
            b13 = a13.charAt(0)-48;
            if(b13>9)
            {
                flag=1;
                c[13]=13;
            }
        String a14 = ID.substring(13,14);
            int b14;
            b14 = a14.charAt(0)-48;
            if(b14>9)
            {
                flag=1;
                c[14]=14;
            }
        String a15 = ID.substring(14,15);
            int b15;
            b15 = a15.charAt(0)-48;
            if(b15>9)
            {
                flag=1;
                c[15]=15;
            }
        String a16 = ID.substring(15,16);
            int b16;
            b16 = a16.charAt(0)-48;
            if(b16>9)
            {
                flag=1;
                c[16]=16;
            }
        String a17 = ID.substring(16,17);
            int b17;
            b17 = a17.charAt(0)-48;
            if(b17>9)
            {
                flag=1;
                c[17]=17;
            }
        String a18 = ID.substring(17,18);
            int b18;
            b18 = a18.charAt(0)-48;
        int sum=b1*7+b2*9+b3*10+b4*5+b5*8+b6*4+b7*2+b8*1+b9*6+b10*3+b11*7+b12*9+b13*10+b14*5+b15*8+b16*4+b17*2;
        int check = sum % 11;
        int check1,check2;
        if(check==0)
        {
            check1=1;
            if(b18==check1)
            {
                out.print("您输入的是正确的身份证号");
            }
            else
            {
                out.print("您输入的身份证号错误!");
            }
        }
        else if(check==1)
        {
            check1=0;
            if(b18==check1)
            {
                out.print("您输入的是正确的身份证号");
            }
            else
            {
                out.print("您输入的身份证号错误!");
            }
        }
        else if(check==2)
        {
            check1=40;
            check2=72;
            if(b18==check1 || b18==check2)
            {
                out.print("您输入的是正确的身份证号");
            }
            else
            {
                out.print("您输入的身份证号错误!");
            }
        }
        else if(check>2)
        {
            int check3=1;
            for(int i=3,j=9;i<=10;i++,j--)
            {
                if(check==i)
                {
                    check1=j;
                    if(b18==check1&&flag==0)
                    {
                        out.print("您输入的是正确的身份证号");
                        check3=0;
                    }
                }
            }
            if(check3==1&&flag==0)
            {
                out.print("您输入的身份证号错误!");
            }
            else if(flag==1) {
                out.print("您输入的身份证号不合法!");
                for (int i = 1; i <= 17; i++) {
                    if(c[i]!=0){
                    out.print("-----且不合法的位数是:第"+c[i]+"位!");
                }}
            }
        }

    }
        else {
            out.print("身份证号位数有问题");
        }
    }
%>

</form>
    <br>
    <br>
</div>
</body>
</html>

  

posted @ 2022-09-26 20:20  回忆也交给时间  阅读(55)  评论(0编辑  收藏  举报