/** 匹配身份证号 规则: 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