C# 常用正则验证[转]

  1  
  2  
  3 using System;
  4 using System.Collections.Generic;
  5 using System.Linq;
  6 using System.Text;
  7 using System.Web;
  8 using System.Web.UI.HtmlControls;
  9 using System.Web.UI;
 10 using System.Text.RegularExpressions;
 11  
 12  
 13 namespace Common
 14 {
 15     public class Validate
 16     {
 17         private static readonly Regex RegPhone = new Regex("(^(\\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}))$)");  //电话号码和手机验证
 18         private static Regex RegEmail = new Regex("^\\s*([A-Za-z0-9_-]+(\\.\\w+)*@([\\w-]+\\.)+\\w{2,3})\\s*$"); //new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");//w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样
 19         private static Regex RegNum = new Regex("^[0-9]+$");
 20  
 21  
 22         //必须是数字正则表达式(小数或整数)
 23         private static Regex regex = new Regex(@"/^[0-9]+\.?[0-9]{0,3}$/");
 24         /// <summary>
 25         /// 身份证正值表达式
 26         /// </summary>
 27         private static readonly Regex RegCardId = new Regex("(^\\d{15}$)|(^\\d{17}([0-9]|X|x)$)");
 28         
 29         #region 确定用户输入是否合法
 30         /// <summary>
 31         /// 确定用户输入是否合法
 32         /// </summary>
 33         /// <param name="text">用户输入字符串</param>
 34         /// <param name="maxLength">最大字符串长度</param>
 35         /// <returns></returns>
 36         public static string InputText(string text, int maxLength)
 37         {
 38             if (string.IsNullOrEmpty(text))
 39                 return string.Empty;
 40             text = text.Trim();
 41             if (maxLength != 0)
 42                 if (text.Length > maxLength)
 43                     text = text.Substring(0, maxLength);
 44             text = Regex.Replace(text, "[\\s]{2,}", " ");
 45             text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n");
 46             text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " ");
 47             //text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); //屏蔽标签
 48             text = text.Replace("'", "''");
 49             return text;
 50         }
 51         #endregion
 52  
 53  
 54         #region 验证电话号码
 55         // 电话号码和手机号码检查
 56         /// <summary>
 57         /// 电话号码和手机号码检查
 58         /// </summary>
 59         /// <param name="inputData">电话号码或手机号码</param>
 60         /// <returns>匹配结果</returns>
 61         public static bool IsPhone(string inputData)
 62         {
 63             Match m = RegPhone.Match(inputData);
 64             return m.Success;
 65         }
 66         #endregion
 67  
 68  
 69         #region 验证参数是否为中文字符
 70         /// <summary>
 71         /// 验证参数是否为中文字符
 72         /// </summary>
 73         /// <param name="input">输入参数</param>
 74         /// <returns></returns>
 75         public static bool IsChinese(string input)
 76         {
 77             Regex regex = new Regex(@"[\u4e00-\u9fa5]", RegexOptions.IgnoreCase);
 78             return regex.IsMatch(input);
 79         }
 80         #endregion
 81  
 82  
 83         #region 邮件地址
 84  
 85  
 86         /// <summary>
 87         /// 邮件地址验证
 88         /// </summary>
 89         /// <param name="inputData">输入字符串</param>
 90         /// <returns>验证结果</returns>
 91         public static bool IsEmail(string inputData)
 92         {
 93             Match m = RegEmail.Match(inputData);
 94             return m.Success;
 95         }
 96         #endregion
 97  
 98  
 99         #region 是否为数字
100         /// <summary>
101         /// 是否为数字
102         /// </summary>
103         /// <param name="inputData">输入字符串</param>
104         /// <returns>是否为数字</returns>
105         public static bool IsNum(string inputData)
106         {
107             if(string.IsNullOrEmpty(inputData))
108             {
109                 return false;
110             }
111             Match m = RegNum.Match(inputData);
112             return m.Success;
113         }
114         /// <summary>
115         /// 判断是否是整数或小数
116         /// </summary>
117         /// <param name="str">输入的字符串</param>
118         /// <returns>是否为数字</returns>
119         public static bool IsNumAll(string str)
120         {
121             if (string.IsNullOrEmpty(str))
122             {
123                 return false;
124             }
125             Match m = regex.Match(str);
126             return m.Success;
127         }
128  
129  
130         #endregion
131  
132  
133         #region 是否为身份证
134         /// <summary>
135         /// 是否为身份证
136         /// </summary>
137         /// <param name="inputData">输入字符串</param>
138         /// <returns>是否为身份证</returns>
139         public static bool IsCardId(string inputData)
140         {
141             Match m = RegCardId.Match(inputData);
142             return m.Success;
143         }
144         #endregion
145  
146  
147         /// <summary>
148         /// 判断字符串是否是纯数字
149         /// </summary>
150         /// <param name="message">源字符串</param>
151         /// <returns></returns>
152         public static bool IsNumberic(string message)//, out int result
153         {
154             System.Text.RegularExpressions.Regex rex =
155             new System.Text.RegularExpressions.Regex(@"^\d+$");
156             var result = -1;
157             if (rex.IsMatch(message))
158             {
159                 result = int.Parse(message);
160                 return true;
161             }
162             else
163                 return false;
164         }
165     }
166 }

猜你喜欢

转载自www.cnblogs.com/Katakana/p/9686239.html
今日推荐