C# 限制问题

这篇博客探讨了C#中的一些限制问题,包括密码的MD5加密、手机号码的11位限制、身份证的有效性验证以及前端文本框输入金额的限制。作者通过调用特定方法进行了详细说明,并提到了随机码的使用。欢迎读者提出更多建议。

关于密码MD5 加密

  1. 在一个方法里调用这个MD5 方法
var pwd = Md5(PassWord);
public static string Md5(string passWord)
        {
            string cl = passWord;
            string pwd = "";
            MD5 md5 = MD5.Create(); 

            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
           
            for (int i = 0; i < s.Length; i++)
            {
                pwd = pwd + s[i].ToString("X");
            }
            return pwd;
           
        }

关于手机号限制11位

			Regex telephone = new Regex("^[0-9]{11,11}$");
            if (!telephone.IsMatch(model.Telephone))
            {
                throw new ArgumentException("请输入正确手机号");
            }

关于身份证有效限制

**

  1. 1.方法里调用这个CheckIDCard18

**

var idCardNo = CheckIDCard18(model.CardInfoID);

 private bool CheckIDCard18(string idNumber)
        {
            long n = 0;

            if (idNumber.Length != 18)
            {
                return false;
            }

            if (long.TryParse(idNumber.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(idNumber.Replace('x', '0').Replace('X', '0'), out n) == false)
            {
                return false;//数字验证  
            }
            if (idNumber.Length != 18)
            {
                return false;
            }

            string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";

            if (address.IndexOf(idNumber.Remove(2)) == -1)
            {
                return false;//省份验证  
            }
            string birth = idNumber.Substring(6, 8).Insert(6, "-").Insert(4, "-");

            DateTime time = new DateTime();
            if (DateTime.TryParse(birth, out time) == false)
            {
                return false;//生日验证  
            }
            string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
            string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');

            char[] Ai = idNumber.Remove(17).ToCharArray();
            int sum = 0;
            for (int i = 0; i < 17; i++)
            {
                sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());

            }
            int y = -1;
            Math.DivRem(sum, 11, out y);
            if (arrVarifyCode[y] != idNumber.Substring(17, 1).ToLower())
            {
                return false;//校验码验证  

            }
            return true;
        }

关于前端文本框输入金额限制

  1. 1.在input属性里调用
οnkeyup="clearNoNum(this)

function clearNoNum(obj) {
         obj.value = obj.value.replace(/[^\d.]/g, ""); //清除"数字""."以外的字符
         obj.value = obj.value.replace(/^\./g, ""); //验证第一个字符是数字而不是
         obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
         obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
         obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
     }

随机码

var random = RandomHelper.GetValue(RandomHelper.RandomType.Num, 6);
  • 陆陆续续还会增加,如果有好的建议可以私信我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值