C#之正则表达式验证

 /** 
        匹配身份证号 
        规则: 
            15位纯数字或者18位纯数字或者17位数字加一位x 
        */ 

             var regex = new System.Text.RegularExpressions.Regex(@"^(([0-9]{15})|([0-9]{18})|([0-9]{17}x))$");
                        if (!regex.IsMatch(orderAdd.ReceiveIdCard))
                        {
                            result.Success = false;
                            result.ErrorMsg = "身份证格式不正确";
                            return Content(JsonConvert.SerializeObject(result, Formatting.None));
                        }

 

                //如果填写手机号,校验手机号格式
                if (!string.IsNullOrEmpty(one[3].ToString()))
                {
                    //^[1][358][0-9]{9}$现在只有13、15和18开头的11位手机号码。以1开头,第2位数字为3或5或8,后面接9位数字。
                    if (!Regex.IsMatch(one[3].ToString(), @"^[1][358][0-9]{9}$"))
                    {
                        rtn.Add(string.Format("第{0}行:手机号码格式不正确", no));
                        continue;
                    }
                }

 

参照:http://blog.csdn.net/eightwhells/article/details/8935390

 

判断是否为数字:

                var regex = new Regex(@"^\d+$");    //正则 判断为数字
                if (regex.IsMatch(model.ProductCondition)) 
                {
                    queryItems.Add(string.Format(" ProductId='{0}'", model.ProductCondition));
                }
                else  //为字符串
                {
                    queryItems.Add(string.Format(" ProductName like '%{0}%'", model.ProductCondition));
                }

 

验证是否是合法手机号

                if (!string.IsNullOrEmpty(one[5].ToString()))
                {
                    if (!Regex.IsMatch(one[5].ToString(), @"^[a-z0-9]([a-z0-9\\.]*[-_]{0,4}?[a-z0-9\\.]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+([\.][\w_-]+){1,5}$"))
                    {
                        rtn.Add(string.Format("第{0}行:邮箱格式不正确", no));
                        continue;
                    }
                }

 

int actualNum = 0;
var isInt = int.TryParse(submitAmount.ToString(), out actualNum);
//正则 判断非负整数
if (isInt && !new Regex(@"^\d+$").IsMatch(submitAmount.ToString()))
{
checkMsg.Add($"第{rowIndex + 1}行,商品条码{dr?.CommodityCode},本次调拨数量在更改后必须是整数;");
num++;
}
else if (!isInt && !new Regex(@"^\d+\.[0](\d{1,2})?$").IsMatch(submitAmount.ToString()))
{
checkMsg.Add($"第{rowIndex + 1}行,商品条码{dr?.CommodityCode},本次调拨数量在更改后必须是整数;");
num++;
}

 

//if (!new Regex(@"^\d+|\.[0](\d{1,2})?$").IsMatch(submitAmount.ToString()))
//{
// checkMsg.Add($"第{rowIndex + 1}行,商品条码{dr?.CommodityCode},本次调拨数量在更改后必须是整数;");
// num++;
//}

https://zhidao.baidu.com/question/154587670.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值