zoukankan      html  css  js  c++  java
  • *身份证验证并与性别联动

    1、<asp:Label runat="server" ID="Label_SUB1_F15" CssClass="liLeft" title="">身份證號<font color="red">&nbsp;*</font></asp:Label><div
            class="contentContainer">
            <asp:TextBox ID="TextBox_SUB1_F15" runat="server" MaxLength="10" onblur="ChkIdNo(this);"
                CssClass="pub_clsInputEdit TextBoxElement IDNO elementDisplayYes">
            </asp:TextBox></div>

        <asp:Label runat="server" ID="Label_SUB1_F17" CssClass="liLeft" title="">性別<font color="red">&nbsp;*</font></asp:Label><div
            class="contentContainer">
            <asp:RadioButtonList ID="RadioButtonList_SUB1_F17" runat="server" CssClass="liRight RadioButtonListElement Sex"
                RepeatDirection="Horizontal" RepeatLayout="Flow" Enabled="true">
            </asp:RadioButtonList>
        </div>

    2、   <script language="javascript" type="text/javascript">
            //身份證號碼验证及与性別聯動
            function ChkIdNo(obj) {
                var txtVal = obj.value;
                if (txtVal != '') {
                    if (txtVal.length < 10) {
                        alert("身份證號碼必須為10位!");
                        obj.focus();
                        obj.select();
                        return false;
                    }
                    else {

                        //检查是否存在
                        var xmlURL = "JsonData.ashx?t=IV&val=" + txtVal;

                        $.get(xmlURL, {}, function(result) {
                            if (result == "N") {
                                alert(document.getElementById("<%=IdErr.ClientID%>").innerText); //不正确 
                                //alert("身份證號碼不合規則!");                           
                                obj.focus();
                            }
                            else {
                                obj.value = obj.value.toUpperCase();
                            }
                        }, "text");
                    }

                    if (txtVal != null) {
                        var secondChar = txtVal.substr(1, 1);
                        if (secondChar != "") {
                            if (secondChar == '1') {
                                var radioList = document.getElementById("<%=RadioButtonList_SUB1_F17.ClientID%>").getElementsByTagName("input");
                                radioList[0].checked = true;
                            }
                            else {
                                var radioList = document.getElementById("<%=RadioButtonList_SUB1_F17.ClientID%>").getElementsByTagName("input");
                                radioList[1].checked = true;
                            }
                        }
                    }

                }
            }
        </script>

    3、添加一个JsonData.ashx文件

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using Benq.Flower.Form.CARRFORM7_V8.DAL;

    namespace Benq.Flower.Form.CARRFORM7_V8
    {
        public class JsonData : IHttpHandler
        {
            public void ProcessRequest(HttpContext context)
            {
                CARRFORM7 C7 = new CARRFORM7_V8.DAL.CARRFORM7();
                context.Response.ContentType = "text/plain";
                string returnValue = string.Empty;

                if (context.Request.QueryString["t"] != null)
                {
                    string t = context.Request.QueryString["t"];
                    string val = context.Request.QueryString["val"];


                    if (t.Equals("IV"))
                    {
                        #region
                        if (C7.ValidateIdentityID(val))
                        {
                            context.Response.Write("Y");
                        }
                        else
                        {
                            context.Response.Write("N");
                        }
                        #endregion
                    }
                }
                context.Response.End();
            }

            public bool IsReusable
            {
                get
                {
                    return true;
                }
            }
        }
    }

    4、验证规则

    #region

            public bool ValidateIdentityID(string IdentityID)
            {
                bool valid = false;
                //先通过正则表达式验证基本格式
                if (new Regex(@"[A-Z][1-2]d{8}").Match(IdentityID).Success)
                {
                    //如果格式正确,再验证最后一位检查码
                    valid = ValidateCheckCharacter(IdentityID);
                }

                return valid;
            }
            //验证检查码是否正确。正确返回true, 否则返回false
            private bool ValidateCheckCharacter(string IdentityID)
            {
                bool valid = false;
                int digitalCharacter, times, multiplyResult, modulos, remainder, digitalCheckCharacter;
                string checkCharacter, checkCharacterInID;


                //1.把区域码转换成数字
                string regionPrefix = IdentityID.Substring(0, 1).ToUpper();
                string regionDigital = this.GetRegionDigitalString(regionPrefix);
                string digitalIdentityID = regionDigital + IdentityID.Remove(0, 1);

                //2.根据ID前9位(10位数字)产生正确的检查码  
                multiplyResult = 0;
                //2.1乘权数相加
                for (int i = 0; i < digitalIdentityID.Length - 1; i++)
                {
                    digitalCharacter = int.Parse(digitalIdentityID.Substring(i, 1));
                    //从第一位到第10位的权数分别为:1,9,8,7,6,5,4,3,2,1
                    if (i == 0)
                    {
                        times = 1;
                    }
                    else
                    {
                        times = 10 - i;
                    }

                    //加权乘再累加
                    multiplyResult += digitalCharacter * times;
                }

                //2.2除以模數10取其餘數
                modulos = 10;
                remainder = multiplyResult % modulos;
                if (remainder == 0)
                {
                    checkCharacterInID = IdentityID.Substring(9, 1);
                    if (remainder.ToString() == checkCharacterInID)
                    {
                        valid = true;
                    }
                }
                else
                {
                    digitalCheckCharacter = modulos - remainder;
                    checkCharacter = digitalCheckCharacter.ToString();

                    //3.与字符串中最后一位检查码比较,一致说明正确,否则错误
                    checkCharacterInID = IdentityID.Substring(9, 1);
                    if (checkCharacter.Equals(checkCharacterInID))
                    {
                        valid = true;
                    }

                }
                return valid;
            }
            //根据区域码得到两位数字.因为转换规律性不强,采用硬编码,可靠
            private string GetRegionDigitalString(string regionPrefix)
            {
                string regionDigital = string.Empty;
                switch (regionPrefix)
                {
                    case "A":
                        regionDigital = "10";
                        break;
                    case "B":
                        regionDigital = "11";
                        break;
                    case "C":
                        regionDigital = "12";
                        break;
                    case "D":
                        regionDigital = "13";
                        break;
                    case "E":
                        regionDigital = "14";
                        break;
                    case "F":
                        regionDigital = "15";
                        break;
                    case "G":
                        regionDigital = "16";
                        break;
                    case "H":
                        regionDigital = "17";
                        break;
                    case "I":
                        regionDigital = "34";
                        break;
                    case "J":
                        regionDigital = "18";
                        break;
                    case "K":
                        regionDigital = "19";
                        break;
                    case "L":
                        regionDigital = "20";
                        break;
                    case "M":
                        regionDigital = "21";
                        break;
                    case "N":
                        regionDigital = "22";
                        break;
                    case "O":
                        regionDigital = "35";
                        break;
                    case "P":
                        regionDigital = "23";
                        break;
                    case "Q":
                        regionDigital = "24";
                        break;
                    case "R":
                        regionDigital = "25";
                        break;
                    case "S":
                        regionDigital = "26";
                        break;
                    case "T":
                        regionDigital = "27";
                        break;
                    case "U":
                        regionDigital = "28";
                        break;
                    case "V":
                        regionDigital = "29";
                        break;
                    case "W":
                        regionDigital = "32";
                        break;
                    case "X":
                        regionDigital = "30";
                        break;
                    case "Y":
                        regionDigital = "31";
                        break;
                    case "Z":
                        regionDigital = "33";
                        break;
                    default:
                        throw new Exception("Invalid region character");
                }

                return regionDigital;
            }


            #endregion

  • 相关阅读:
    Silverlight C# 游戏开发:Flyer05与什么什么进行搏斗
    Silverlight C# 游戏开发:Flyer07做一个有开始的游戏
    Silverlight C# 游戏开发:面向对象在游戏中的实例(一)
    Silverlight C# 游戏开发:面向对象在游戏中的实例(二)
    Silverlight C# 游戏开发:Flyer06小小的改进让游戏更有趣
    linux网络命令ip\route\links回顾
    Google Style的C++编码规范
    TCP/IP协议和IP组播的视频传输
    Multicast server and client in Python
    用户profile中umask码的含义详解(默认是022)
  • 原文地址:https://www.cnblogs.com/Chinarain/p/3821936.html
Copyright © 2011-2022 走看看