zoukankan      html  css  js  c++  java
  • C#实现身份证号码校验的类

    public class CardValidator
        {
            private const int Divisor = 11;
            private const int MinLen = 17;
            private static readonly List<int>  Factors=new List<int>()
            {
                7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2
            };
            private static readonly Dictionary<int,string> Result=new Dictionary<int, string>()
            {
                {0,"1"},{1,"0"},{2,"X"},{3,"9"},{4,"9"},{5,"7"},{6,"6"},{7,"5"},{8,"4"},{9,"3"},{10,"2"}
            }; 
            private static readonly List<int> Products=new List<int>(); 
    
            public static bool IsValid(string cardNo)
            {
                if (string.IsNullOrEmpty(cardNo) || cardNo.Length <MinLen)
                {
                    return false;
                }
    
                var subCard = cardNo.Substring(0, MinLen);
                for (var i=0;i<subCard.Length;i++)
                {
                    int c;
                    if (!int.TryParse(subCard.Substring(i,1), out c))
                    {
                        return false;
                    }
                    Products.Add(c*Factors[i]);
                }
    
                var sum = Products.Sum();
                var mod = sum%Divisor;
                var val = Result[mod];
    
                return val == cardNo.Substring(cardNo.Length - 1, 1);
            }
        }
  • 相关阅读:
    touch
    ls
    Linux基础
    errors
    strconv
    strings
    fmt
    os/exec
    笔记本连不上网怎么办
    笔记本连不上网怎么办
  • 原文地址:https://www.cnblogs.com/iot1024/p/5946235.html
Copyright © 2011-2022 走看看