c# 校验文本框的正则

时间:2021-08-24 18:08:36
12345678910111213141516171819202122232425262728293031323334 #region 常用数据验证的封装,数字字符的验证       /// <summary>       /// 常用数据验证的封装,数字字符的验证       /// </summary>       /// <param name="inputVal">需要验证的数值【字符串,或者数字】</param>       /// <param name="type">类型为哪一个验证</param>       /// <returns>如果验证成功则返回True,否则返回false</returns>       public static bool IsMatch(string inputVal, int type)       {           switch (type)           {               case 0:                   return Regex.IsMatch(inputVal, @"^[1-9]d*$");  //匹配正整数               case 1:                   return Regex.IsMatch(inputVal, @"^-?\d+$");  //匹配整数               case 2:                   return Regex.IsMatch(inputVal, @"^[A-Za-z0-9]+$");  //匹配由数字和26个英文字母组成的字符串                case 3:                   return Regex.IsMatch(inputVal, @"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$");  //匹配正浮点数               case 4:                   return Regex.IsMatch(inputVal, @"^[\u4e00-\u9fa5]{0,}$");  //匹配汉字               case 5:                   return Regex.IsMatch(inputVal, @"^[0-9]+(.[0-9]{1,3})?$");  //匹配1~3位小数的正实数               case 6:                   return Regex.IsMatch(inputVal, @"^[A-Za-z]+$");  //匹配英文字符               case 7:                   return Regex.IsMatch(inputVal, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");   //验证邮箱               case 8:                   return Regex.IsMatch(inputVal, @"((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)");   //验证手机号码               default:                   return true;           }       }       #endregion