Example #1
0
        /// <summary>
        /// 验证身份证是否正确
        /// </summary>
        /// <param name="idNumber">待验证的证件号码</param>
        /// <param name="minYear">允许最小年份,默认0</param>
        /// <param name="validLimit">验证区域级别,默认AreaValidLimit.Province</param>
        /// <param name="ignoreCheckBit">是否忽略校验位验证,默认false</param>
        /// <returns>验证结果</returns>
        public IDValidationResult Validate(string idNumber, ushort minYear = 0, AreaValidLimit validLimit = AreaValidLimit.Province, bool ignoreCheckBit = false)
        {
            var result = base.Validate(idNumber);
            var valid  = result.IsValid &&
                         this.ValidBirthday(idNumber, minYear, result) &&
                         this.ValidArea(idNumber, validLimit, result) &&
                         this.ValidCheckBit(idNumber, ignoreCheckBit, result) &&
                         this.ValidOtherInfo(idNumber, result);

            return(result);
        }
Example #2
0
        /// <summary>
        /// 验证行政区划
        /// </summary>
        /// <param name="idNumber"></param>
        /// <param name="validLimit"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        protected virtual bool ValidArea(string idNumber, AreaValidLimit validLimit, IDValidationResult result)
        {
            int  areaNumber = this.GetAreaNumber(idNumber);
            var  area       = AreaHelper.GetDeepestArea(areaNumber, this.Dictionary);
            bool valid      = area != null && area.GetDepth() >= (int)validLimit;

            result.RecognizableArea = area;
            result.AreaNumber       = areaNumber;
            if (!valid)
            {
                if (area == null)
                {
                    result.AddErrorMessage(ErrorMessage.InvalidArea);
                }
                else
                {
                    result.AddErrorMessage(ErrorMessage.AreaLimitOutOfRange, validLimit);
                }
            }
            return(valid);
        }
        /// <summary>
        /// 验证身份证是否正确
        /// </summary>
        /// <param name="idNumber">待验证的证件号码</param>
        /// <param name="minYear">允许最小年份,默认0</param>
        /// <param name="validLength">要验证的证件长度,默认不指定null</param>
        /// <param name="validLimit">验证区域级别,默认AreaValidLimit.Province</param>
        /// <param name="ignoreCheckBit">是否忽略校验位验证,默认false</param>
        /// <returns>验证结果</returns>
        public static IDValidationResult Validate(this string idNumber, ushort minYear = 0, int?validLength = null, AreaValidLimit validLimit = AreaValidLimit.Province, bool ignoreCheckBit = false)
        {
            IIDValidator validator = null;

            _ = ValidatorHelper.ValidEmpty(idNumber, out IDValidationResult result, ErrorMessage.Empty) &&
                ValidatorHelper.ValidLength(idNumber, validLength, ErrorMessage.LengthOutOfRange, result) &&
                ValidImplement(idNumber, result, out validator);
            return(validator == null ? result : validator.Validate(idNumber, minYear, validLimit, ignoreCheckBit));
        }