您现在的位置是: 首页 >  .NET开发 >  文章详情 文章详情

身份证号验证

2019-08-19 【.NET开发】 1416人浏览

public class ValidateIdNum

    {        #region 验证身份证  
        public static bool CheckIDCard(string Id)
        {            if (string.IsNullOrWhiteSpace(Id))                return false;            if (Id.Length == 18)
            {                bool check = CheckIDCard18(Id);                return check;
            }            else if (Id.Length == 15)
            {                bool check = CheckIDCard15(Id);                return check;
            }            else
            {                return false;
            }
        }        private static bool CheckIDCard18(string Id)
        {            long n = 0;            if (long.TryParse(Id.Remove(17), out n) == false || n 

[AttributeUsageAttribute(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]    public class IdNumberAttribute : ValidationAttribute
    {        public override bool IsValid(object value)
        {            if (value == null)
            {                return false;
            }            string text = value as string;            return ValidateIdNum.CheckIDCard(text);
        }

    }



很赞哦! (0)

站长推荐