c# 校验文本框的正则
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#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
posted on 2015-06-29 10:32  zrSoldier  阅读(858)  评论(0编辑  收藏  举报