懒羊羊
2023-12-28 e46d3baaf3e8d7d85f4bafec3aad75e52b078408
提交 | 用户 | 时间
e46d3b 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Text.RegularExpressions;
6
7 namespace YX.Common.DoNetCode
8 {
9     /// <summary>
10     /// 各种输入格式验证辅助类
11     /// </summary>
12     public class ValidateUtil
13     {
14         private static Regex RegNumber = new Regex("^[0-9]+$");
15         private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
16         private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
17         private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$
18         private static Regex RegEmail = new Regex(@"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$");//w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样 
19         private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");
20
21         #region 用户名密码格式
22
23         /// <summary>
24         /// 返回字符串真实长度, 1个汉字长度为2
25         /// </summary>
26         /// <returns>字符长度</returns>
27         public static int GetStringLength(string stringValue)
28         {
29             return Encoding.Default.GetBytes(stringValue).Length;
30         }
31
32         /// <summary>
33         /// 检测用户名格式是否有效
34         /// </summary>
35         /// <param name="userName"></param>
36         /// <returns></returns>
37         public static bool IsValidUserName(string userName)
38         {
39             int userNameLength = GetStringLength(userName);
40             if (userNameLength >= 4 && userNameLength <= 20 && Regex.IsMatch(userName, @"^([\u4e00-\u9fa5A-Za-z_0-9]{0,})$"))
41             {   // 判断用户名的长度(4-20个字符)及内容(只能是汉字、字母、下划线、数字)是否合法
42                 return true;
43             }
44             else
45             {
46                 return false;
47             }
48         }
49
50         /// <summary>
51         /// 密码有效性
52         /// </summary>
53         /// <param name="password"></param>
54         /// <returns></returns>
55         public static bool IsValidPassword(string password)
56         {
57             return Regex.IsMatch(password, @"^[A-Za-z_0-9]{6,16}$");
58         }
59         #endregion
60
61         #region 数字字符串检查
62
63         /// <summary>
64         /// int有效性
65         /// </summary>
66         /// <param name="val"></param>
67         /// <returns></returns>
68         static public bool IsValidInt(string val)
69         {
70             return Regex.IsMatch(val, @"^[1-9]\d*\.?[0]*$");
71         }
72
73         /// <summary>
74         /// 是否数字字符串
75         /// </summary>
76         /// <param name="inputData">输入字符串</param>
77         /// <returns></returns>
78         public static bool IsNumber(string inputData)
79         {
80             Match m = RegNumber.Match(inputData);
81             return m.Success;
82         }
83
84         /// <summary>
85         /// 是否数字字符串 可带正负号
86         /// </summary>
87         /// <param name="inputData">输入字符串</param>
88         /// <returns></returns>
89         public static bool IsNumberSign(string inputData)
90         {
91             Match m = RegNumberSign.Match(inputData);
92             return m.Success;
93         }
94
95         /// <summary>
96         /// 是否是浮点数
97         /// </summary>
98         /// <param name="inputData">输入字符串</param>
99         /// <returns></returns>
100         public static bool IsDecimal(string inputData)
101         {
102             Match m = RegDecimal.Match(inputData);
103             return m.Success;
104         }
105
106         /// <summary>
107         /// 是否是浮点数 可带正负号
108         /// </summary>
109         /// <param name="inputData">输入字符串</param>
110         /// <returns></returns>
111         public static bool IsDecimalSign(string inputData)
112         {
113             Match m = RegDecimalSign.Match(inputData);
114             return m.Success;
115         }
116
117         #endregion
118
119         #region 中文检测
120
121         /// <summary>
122         /// 检测是否有中文字符
123         /// </summary>
124         /// <param name="inputData"></param>
125         /// <returns></returns>
126         public static bool IsHasCHZN(string inputData)
127         {
128             Match m = RegCHZN.Match(inputData);
129             return m.Success;
130         }
131
132         /// <summary> 
133         /// 检测含有中文字符串的实际长度 
134         /// </summary> 
135         /// <param name="str">字符串</param> 
136         public static int GetCHZNLength(string inputData)
137         {
138             System.Text.ASCIIEncoding n = new System.Text.ASCIIEncoding();
139             byte[] bytes = n.GetBytes(inputData);
140
141             int length = 0; // l 为字符串之实际长度 
142             for (int i = 0; i <= bytes.Length - 1; i++)
143             {
144                 if (bytes[i] == 63) //判断是否为汉字或全脚符号 
145                 {
146                     length++;
147                 }
148                 length++;
149             }
150             return length;
151
152         }
153
154         #endregion
155
156         #region 常用格式
157
158         /// <summary>
159         /// 验证身份证是否合法  15 和  18位两种
160         /// </summary>
161         /// <param name="idCard">要验证的身份证</param>
162         public static bool IsIdCard(string idCard)
163         {
164             if (string.IsNullOrEmpty(idCard))
165             {
166                 return false;
167             }
168
169             if (idCard.Length == 15)
170             {
171                 return Regex.IsMatch(idCard, @"^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$");
172             }
173             else if (idCard.Length == 18)
174             {
175                 return Regex.IsMatch(idCard, @"^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[A-Z])$", RegexOptions.IgnoreCase);
176             }
177             else
178             {
179                 return false;
180             }
181         }
182
183         /// <summary>
184         /// 是否是邮件地址
185         /// </summary>
186         /// <param name="inputData">输入字符串</param>
187         /// <returns></returns>
188         public static bool IsEmail(string inputData)
189         {
190             Match m = RegEmail.Match(inputData);
191             return m.Success;
192         }
193
194         /// <summary>
195         /// 邮编有效性
196         /// </summary>
197         /// <param name="zip"></param>
198         /// <returns></returns>
199         public static bool IsValidZip(string zip)
200         {
201             Regex rx = new Regex(@"^\d{6}$", RegexOptions.None);
202             Match m = rx.Match(zip);
203             return m.Success;
204         }
205
206         /// <summary>
207         /// 固定电话有效性
208         /// </summary>
209         /// <param name="phone"></param>
210         /// <returns></returns>
211         public static bool IsValidPhone(string phone)
212         {
213             Regex rx = new Regex(@"^(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}$", RegexOptions.None);
214             Match m = rx.Match(phone);
215             return m.Success;
216         }
217
218         /// <summary>
219         /// 手机有效性
220         /// </summary>
221         /// <param name="strMobile"></param>
222         /// <returns></returns>
223         public static bool IsValidMobile(string mobile)
224         {
225             Regex rx = new Regex(@"^(13|15)\d{9}$", RegexOptions.None);
226             Match m = rx.Match(mobile);
227             return m.Success;
228         }
229
230         /// <summary>
231         /// 电话有效性(固话和手机 )
232         /// </summary>
233         /// <param name="strVla"></param>
234         /// <returns></returns>
235         public static bool IsValidPhoneAndMobile(string number)
236         {
237             Regex rx = new Regex(@"^(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}$|^(13|15)\d{9}$", RegexOptions.None);
238             Match m = rx.Match(number);
239             return m.Success;
240         }
241
242         /// <summary>
243         /// Url有效性
244         /// </summary>
245         /// <param name="url"></param>
246         /// <returns></returns>
247         static public bool IsValidURL(string url)
248         {
249             return Regex.IsMatch(url, @"^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*[^\.\,\)\(\s]$");
250         }
251
252         /// <summary>
253         /// IP有效性
254         /// </summary>
255         /// <param name="ip"></param>
256         /// <returns></returns>
257         public static bool IsValidIP(string ip)
258         {
259             return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
260         }
261
262         /// <summary>
263         /// domain 有效性
264         /// </summary>
265         /// <param name="host">域名</param>
266         /// <returns></returns>
267         public static bool IsValidDomain(string host)
268         {
269             Regex r = new Regex(@"^\d+$");
270             if (host.IndexOf(".") == -1)
271             {
272                 return false;
273             }
274             return r.IsMatch(host.Replace(".", string.Empty)) ? false : true;
275         }
276
277         /// <summary>
278         /// 判断是否为base64字符串
279         /// </summary>
280         /// <param name="str"></param>
281         /// <returns></returns>
282         public static bool IsBase64String(string str)
283         {
284             return Regex.IsMatch(str, @"[A-Za-z0-9\+\/\=]");
285         }
286
287         /// <summary>
288         /// 验证字符串是否是GUID
289         /// </summary>
290         /// <param name="guid">字符串</param>
291         /// <returns></returns>
292         public static bool IsGuid(string guid)
293         {
294             if (string.IsNullOrEmpty(guid))
295                 return false;
296
297             return Regex.IsMatch(guid, "[A-F0-9]{8}(-[A-F0-9]{4}){3}-[A-F0-9]{12}|[A-F0-9]{32}", RegexOptions.IgnoreCase);
298         }
299
300         #endregion
301
302         #region 日期检查
303
304         /// <summary>
305         /// 判断输入的字符是否为日期
306         /// </summary>
307         /// <param name="strValue"></param>
308         /// <returns></returns>
309         public static bool IsDate(string strValue)
310         {
311             return Regex.IsMatch(strValue, @"^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))");
312         }
313
314         /// <summary>
315         /// 判断输入的字符是否为日期,如2004-07-12 14:25|||1900-01-01 00:00|||9999-12-31 23:59
316         /// </summary>
317         /// <param name="strValue"></param>
318         /// <returns></returns>
319         public static bool IsDateHourMinute(string strValue)
320         {
321             return Regex.IsMatch(strValue, @"^(19[0-9]{2}|[2-9][0-9]{3})-((0(1|3|5|7|8)|10|12)-(0[1-9]|1[0-9]|2[0-9]|3[0-1])|(0(4|6|9)|11)-(0[1-9]|1[0-9]|2[0-9]|30)|(02)-(0[1-9]|1[0-9]|2[0-9]))\x20(0[0-9]|1[0-9]|2[0-3])(:[0-5][0-9]){1}$");
322         }
323
324         #endregion
325
326         #region 其他
327
328         /// <summary>
329         /// 检查字符串最大长度,返回指定长度的串
330         /// </summary>
331         /// <param name="sqlInput">输入字符串</param>
332         /// <param name="maxLength">最大长度</param>
333         /// <returns></returns>            
334         public static string CheckMathLength(string inputData, int maxLength)
335         {
336             if (inputData != null && inputData != string.Empty)
337             {
338                 inputData = inputData.Trim();
339                 if (inputData.Length > maxLength)//按最大长度截取字符串
340                 {
341                     inputData = inputData.Substring(0, maxLength);
342                 }
343             }
344             return inputData;
345         }
346
347         /// <summary>
348         /// 转换成 HTML code
349         /// </summary>
350         /// <param name="str">string</param>
351         /// <returns>string</returns>
352         public static string Encode(string str)
353         {
354             str = str.Replace("&", "&amp;");
355             str = str.Replace("'", "''");
356             str = str.Replace("\"", "&quot;");
357             str = str.Replace(" ", "&nbsp;");
358             str = str.Replace("<", "&lt;");
359             str = str.Replace(">", "&gt;");
360             str = str.Replace("\n", "<br>");
361             return str;
362         }
363         /// <summary>
364         ///解析html成 普通文本
365         /// </summary>
366         /// <param name="str">string</param>
367         /// <returns>string</returns>
368         public static string Decode(string str)
369         {
370             str = str.Replace("<br>", "\n");
371             str = str.Replace("&gt;", ">");
372             str = str.Replace("&lt;", "<");
373             str = str.Replace("&nbsp;", " ");
374             str = str.Replace("&quot;", "\"");
375             return str;
376         }
377
378         #endregion
379     }
380 }