校验帮助类

一、引用

1 using UtilityHelper.Model;//自定义的实体存放位置
2 using System;
3 using System.IO;
4 using System.Text;
5 using System.Text.RegularExpressions;
6 using System.Web;
View Code

二、使用

1、帮助类

  1         #region 图片检测类
  2         /// <summary>
  3         /// 图片检测类
  4         /// </summary>
  5         public static class ImageValidation
  6         {
  7             #region 上传前的图片是否可靠
  8             /// <summary>
  9             /// 上传前的图片是否可靠
 10             /// </summary>
 11             public static bool IsSecureUploadPhoto(HttpPostedFile oFile)
 12             {
 13                 bool isPhoto = false;
 14                 //获取图片后缀判断
 15                 string fileExtension = System.IO.Path.GetExtension(oFile.FileName).ToLower();
 16                 string[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg", ".bmp" };
 17                 for (int i = 0; i < allowedExtensions.Length; i++)
 18                 {
 19                     if (fileExtension == allowedExtensions[i])
 20                     {
 21                         isPhoto = true;
 22                         break;
 23                     }
 24                 }
 25                 if (!isPhoto)
 26                 {
 27                     return false;
 28                 }
 29                 //通过图片的格式的头文件判断
 30                 EnumFileExtension[] fe = { EnumFileExtension.BMP, EnumFileExtension.GIF, EnumFileExtension.JPG, EnumFileExtension.PNG };
 31                 if (FileTop.IsAllowedExtension(oFile, fe))
 32                     return true;
 33                 else
 34                     return false;
 35             }
 36             #endregion
 37 
 38             #region 上传后的图片是否安全
 39             /// <summary>
 40             /// 上传后的图片是否安全
 41             /// </summary>
 42             /// <param name="photoFile">物理地址</param>
 43             public static bool IsSecureUpfilePhoto(string photoFile)
 44             {
 45                 bool isPhoto = false;
 46                 string Img = "Yes";
 47                 string fileExtension = Path.GetExtension(photoFile).ToLower();
 48                 string[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg", ".bmp" };
 49                 for (int i = 0; i < allowedExtensions.Length; i++)
 50                 {
 51                     if (fileExtension == allowedExtensions[i])
 52                     {
 53                         isPhoto = true;
 54                         break;
 55                     }
 56                 }
 57 
 58                 if (!isPhoto)
 59                 {
 60                     return true;
 61                 }
 62                 StreamReader sr = new StreamReader(photoFile, System.Text.Encoding.Default);
 63                 string strContent = sr.ReadToEnd();
 64                 sr.Close();
 65                 string str = "request|<script|.getfolder|.createfolder|.deletefolder|.createdirectory|.deletedirectory|.saveas|wscript.shell|script.encode|server.|.createobject|execute|activexobject|language=";
 66                 foreach (string s in str.Split('|'))
 67                 {
 68                     if (strContent.ToLower().IndexOf(s) != -1)
 69                     {
 70                         File.Delete(photoFile);
 71                         Img = "No";
 72                         break;
 73                     }
 74                 }
 75                 return (Img == "Yes");
 76             }
 77             #endregion
 78         } 
 79         #endregion
 80 
 81         #region 头文件检测
 82         /// <summary>
 83         /// 头文件检测
 84         /// </summary>
 85         public static class FileTop
 86         {
 87             #region 通过文件的头文件判断是否是允许上传的类型
 88             /// <summary>
 89             /// 通过文件的头文件判断是否是允许上传的类型
 90             /// </summary>
 91             public static bool IsAllowedExtension(HttpPostedFile oFile, EnumFileExtension[] fileEx)
 92             {
 93                 int fileLen = oFile.ContentLength;
 94                 byte[] imgArray = new byte[fileLen];
 95                 oFile.InputStream.Read(imgArray, 0, fileLen);
 96                 MemoryStream ms = new MemoryStream(imgArray);
 97                 BinaryReader br = new BinaryReader(ms);
 98                 string fileclass = string.Empty;
 99                 try
100                 {
101                     var buffer = br.ReadByte();
102                     fileclass = buffer.ToString();
103                     buffer = br.ReadByte();
104                     fileclass += buffer.ToString();
105                 }
106                 catch { }
107                 br.Close();
108                 ms.Close();
109                 foreach (EnumFileExtension fe in fileEx)
110                 {
111                     if (Int32.Parse(fileclass) == (int)fe)
112                         return true;
113                 }
114                 return false;
115             }
116             #endregion
117         } 
118         #endregion
119 
120         #region Excel检测
121         public static class ExcelValidation
122         {
123             #region 上传前的excel是否可靠
124             /// <summary>
125             /// 上传前的excel是否可靠
126             /// </summary>
127             public static bool IsSecureUploadExcel(HttpPostedFile oFile)
128             {
129                 bool isPhoto = false;
130                 //获取图片后缀判断
131                 string fileExtension = System.IO.Path.GetExtension(oFile.FileName).ToLower();
132                 string[] allowedExtensions = { ".xls", ".xlsx" };
133                 for (int i = 0; i < allowedExtensions.Length; i++)
134                 {
135                     if (fileExtension == allowedExtensions[i])
136                     {
137                         isPhoto = true;
138                         break;
139                     }
140                 }
141                 if (!isPhoto)
142                 {
143                     return true;
144                 }
145                 //通过图片的格式的头文件判断
146                 EnumFileExtension[] fe = { EnumFileExtension.XLS, EnumFileExtension.XLSX };
147                 if (FileTop.IsAllowedExtension(oFile, fe))
148                     return true;
149                 else
150                     return false;
151             }
152             #endregion
153 
154             #region 上传后的excel是否安全
155             /// <summary>
156             /// 上传后的excel是否安全
157             /// </summary>
158             /// <param name="photoFile">物理地址</param>
159             public static bool IsSecureUpfileExcel(string photoFile)
160             {
161                 bool isPhoto = false;
162                 string Img = "Yes";
163                 string fileExtension = Path.GetExtension(photoFile).ToLower();
164                 string[] allowedExtensions = { ".xls", ".xlsx" };
165                 for (int i = 0; i < allowedExtensions.Length; i++)
166                 {
167                     if (fileExtension == allowedExtensions[i])
168                     {
169                         isPhoto = true;
170                         break;
171                     }
172                 }
173 
174                 if (!isPhoto)
175                 {
176                     return true;
177                 }
178                 StreamReader sr = new StreamReader(photoFile, System.Text.Encoding.Default);
179                 string strContent = sr.ReadToEnd();
180                 sr.Close();
181                 string str = "request|<script|.getfolder|.createfolder|.deletefolder|.createdirectory|.deletedirectory|.saveas|wscript.shell|script.encode|server.|.createobject|execute|activexobject|language=";
182                 foreach (string s in str.Split('|'))
183                 {
184                     if (strContent.ToLower().IndexOf(s) != -1)
185                     {
186                         File.Delete(photoFile);
187                         Img = "No";
188                         break;
189                     }
190                 }
191                 return (Img == "Yes");
192             }
193             #endregion
194         }
195         #endregion
196 
197         #region 正则表达式变量
198         private static Regex RegPhone = new Regex("^[0-9]+[-]?[0-9]+[-]?[0-9]$");
199         private static Regex RegNumber = new Regex("^[0-9]+$");
200         private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
201         private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
202         private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$
203         private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");//w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样 
204         private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");
205         #endregion
206          
207         #region 字符串检查
208         /// <summary>
209         /// 检查字符串最大长度,返回指定长度的串
210         /// </summary>
211         /// <param name="input">输入字符串</param>
212         /// <param name="maxLength">最大长度</param>
213         /// <returns></returns>            
214         public static string GetSubStr(string input, int maxLength)
215         {
216             if (input != null && input != string.Empty)
217             {
218                 input = input.Trim();
219                 if (input.Length > maxLength)//按最大长度截取字符串
220                     input = input.Substring(0, maxLength);
221             }
222             return input;
223         } 
224         #endregion
225 
226         #region 数字字符串检查
227         public static bool IsPhone(string inputData)
228         {
229             Match m = RegPhone.Match(inputData);
230             return m.Success;
231         }
232         /// <summary>
233         /// 检查Request查询字符串的键值,是否是数字,最大长度限制
234         /// </summary>
235         /// <param name="req">Request</param>
236         /// <param name="inputKey">Request的键值</param>
237         /// <param name="maxLen">最大长度</param>
238         /// <returns>返回Request查询字符串</returns>
239         public static string FetchInputDigit(HttpRequest req, string inputKey, int maxLen)
240         {
241             string retVal = string.Empty;
242             if (inputKey != null && inputKey != string.Empty)
243             {
244                 retVal = req.QueryString[inputKey];
245                 if (null == retVal)
246                     retVal = req.Form[inputKey];
247                 if (null != retVal)
248                 {
249                     retVal = GetSubStr(retVal, maxLen);
250                     if (!IsNumber(retVal))
251                         retVal = string.Empty;
252                 }
253             }
254             if (retVal == null)
255                 retVal = string.Empty;
256             return retVal;
257         }
258         /// <summary>
259         /// 是否数字字符串
260         /// </summary>
261         /// <param name="inputData">输入字符串</param>
262         /// <returns></returns>
263         public static bool IsNumber(string inputData)
264         {
265             Match m = RegNumber.Match(inputData);
266             return m.Success;
267         }
268 
269         /// <summary>
270         /// 是否数字字符串 可带正负号
271         /// </summary>
272         /// <param name="inputData">输入字符串</param>
273         /// <returns></returns>
274         public static bool IsNumberSign(string inputData)
275         {
276             Match m = RegNumberSign.Match(inputData);
277             return m.Success;
278         }
279         /// <summary>
280         /// 是否是浮点数
281         /// </summary>
282         /// <param name="inputData">输入字符串</param>
283         /// <returns></returns>
284         public static bool IsDecimal(string inputData)
285         {
286             Match m = RegDecimal.Match(inputData);
287             return m.Success;
288         }
289         /// <summary>
290         /// 是否是浮点数 可带正负号
291         /// </summary>
292         /// <param name="inputData">输入字符串</param>
293         /// <returns></returns>
294         public static bool IsDecimalSign(string inputData)
295         {
296             Match m = RegDecimalSign.Match(inputData);
297             return m.Success;
298         }
299 
300         #endregion
301 
302         #region 中文检测
303 
304         /// <summary>
305         /// 检测是否有中文字符
306         /// </summary>
307         /// <param name="inputData"></param>
308         /// <returns></returns>
309         public static bool IsHasCHZN(string inputData)
310         {
311             Match m = RegCHZN.Match(inputData);
312             return m.Success;
313         }
314 
315         #endregion  
316 
317         #region 日期格式判断
318         /// <summary>
319         /// 日期格式字符串判断
320         /// </summary>
321         /// <param name="str"></param>
322         /// <returns></returns>
323         public static bool IsDateTime(string str)
324         {
325             try
326             {
327                 if (!string.IsNullOrEmpty(str))
328                 {
329                     DateTime.Parse(str);
330                     return true;
331                 }
332                 else
333                 {
334                     return false;
335                 }
336             }
337             catch
338             {
339                 return false;
340             }
341         }
342         #endregion
343 
344         #region   检测字符串长度
345         /// <summary>   
346         /// 计算文本长度,区分中英文字符,中文算两个长度,英文算一个长度
347         /// </summary>
348         /// <param name="Text">需计算长度的字符串</param>
349         /// <returns>int</returns>
350         public static int Text_Length(string Text)
351         {
352             int len = 0;
353 
354             for (int i = 0; i < Text.Length; i++)
355             {
356                 byte[] byte_len = Encoding.Default.GetBytes(Text.Substring(i, 1));
357                 if (byte_len.Length > 1)
358                     len += 2;  //如果长度大于1,是中文,占两个字节,+2
359                 else
360                     len += 1;  //如果长度等于1,是英文,占一个字节,+1
361             }
362 
363             return len;
364         }
365         #endregion
366 
367         #region 是否由特定字符组成
368         public static bool isContainSameChar(string strInput)
369         {
370             string charInput = string.Empty;
371             if (!string.IsNullOrEmpty(strInput))
372             {
373                 charInput = strInput.Substring(0, 1);
374             }
375             return isContainSameChar(strInput, charInput, strInput.Length);
376         }
377 
378         public static bool isContainSameChar(string strInput, string charInput, int lenInput)
379         {
380             if (string.IsNullOrEmpty(charInput))
381             {
382                 return false;
383             }
384             else
385             {
386                 Regex RegNumber = new Regex(string.Format("^([{0}])+$", charInput));
387                 //Regex RegNumber = new Regex(string.Format("^([{0}]{{1}})+$", charInput,lenInput));
388                 Match m = RegNumber.Match(strInput);
389                 return m.Success;
390             }
391         }
392         #endregion
393 
394         #region 检查输入的参数是不是某些定义好的特殊字符:这个方法目前用于密码输入的安全检查
395         /// <summary>
396         /// 检查输入的参数是不是某些定义好的特殊字符:这个方法目前用于密码输入的安全检查
397         /// </summary>
398         public static bool isContainSpecChar(string strInput)
399         {
400             string[] list = new string[] { "123456", "654321" };
401             bool result = new bool();
402             for (int i = 0; i < list.Length; i++)
403             {
404                 if (strInput == list[i])
405                 {
406                     result = true;
407                     break;
408                 }
409             }
410             return result;
411         }
412         #endregion
413 
414         #region 验证邮箱
415         /// <summary>
416         /// 验证邮箱
417         /// </summary>
418         /// <param name="source"></param>
419         /// <returns></returns>
420         public static bool IsEmail(string source)
421         {
422             return Regex.IsMatch(source, @"^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$", RegexOptions.IgnoreCase);
423         }
424         public static bool HasEmail(string source)
425         {
426             return Regex.IsMatch(source, @"[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})", RegexOptions.IgnoreCase);
427         }
428         #endregion
429 
430         #region 验证网址
431         /// <summary>
432         /// 验证网址
433         /// </summary>
434         /// <param name="source"></param>
435         /// <returns></returns>
436         public static bool IsUrl(string source)
437         {
438             return Regex.IsMatch(source, @"^(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)://)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(/[a-zA-Z0-9\&amp;%_\./-~-]*)?$", RegexOptions.IgnoreCase);
439         }
440         public static bool HasUrl(string source)
441         {
442             return Regex.IsMatch(source, @"(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)://)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(/[a-zA-Z0-9\&amp;%_\./-~-]*)?", RegexOptions.IgnoreCase);
443         }
444         #endregion 
445 
446         #region 验证手机号
447         /// <summary>
448         /// 验证手机号
449         /// </summary>
450         /// <param name="source"></param>
451         /// <returns></returns>
452         public static bool IsMobile(string source)
453         {
454             return Regex.IsMatch(source, @"^((13[0-9])|(14[5|7|9])|(15([0-3]|[5-9]))|(17[0|3|5-8])|(18[0-9]))\d{8}$", RegexOptions.IgnoreCase);
455         }
456         public static bool HasMobile(string source)
457         {
458             return Regex.IsMatch(source, @"^((13[0-9])|(14[5|7|9])|(15([0-3]|[5-9]))|(17[0|3|5-8])|(18[0-9]))\d{8}", RegexOptions.IgnoreCase);
459         }
460         #endregion
461 
462         #region 验证IP
463         /// <summary>
464         /// 验证IP
465         /// </summary>
466         /// <param name="source"></param>
467         /// <returns></returns>
468         public static bool IsIP(string source)
469         {
470             return Regex.IsMatch(source, @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$", RegexOptions.IgnoreCase);
471         }
472         public static bool HasIP(string source)
473         {
474             return Regex.IsMatch(source, @"(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])", RegexOptions.IgnoreCase);
475         }
476         public static bool IsIp(string ip)
477         {
478             bool result = false;
479             try
480             {
481                 string[] iparg = ip.Split('.');
482                 if (string.Empty != ip && ip.Length < 16 && iparg.Length == 4)
483                 {
484                     int intip;
485                     for (int i = 0; i < 4; i++)
486                     {
487                         intip = Convert.ToInt16(iparg[i]);
488                         if (intip > 255)
489                         {
490                             result = false;
491                             return result;
492                         }
493                     }
494                     result = true;
495                 }
496             }
497             catch
498             {
499                 return result;
500             }
501             return result;
502         }
503         #endregion
504 
505         #region 验证身份证是否有效
506         /// <summary>
507         /// 验证身份证是否有效
508         /// </summary>
509         /// <param name="Id"></param>
510         /// <returns></returns>
511         public static bool IsIDCard(string Id)
512         {
513             if (Id.Length == 18)
514             {
515                 bool check = IsIDCard18(Id);
516                 return check;
517             }
518             else if (Id.Length == 15)
519             {
520                 bool check = IsIDCard15(Id);
521                 return check;
522             }
523             else
524             {
525                 return false;
526             }
527         }
528         public static bool IsIDCard18(string Id)
529         {
530             long n = 0;
531             if (long.TryParse(Id.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(Id.Replace('x', '0').Replace('X', '0'), out n) == false)
532             {
533                 return false;//数字验证
534             }
535             string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
536             if (address.IndexOf(Id.Remove(2)) == -1)
537             {
538                 return false;//省份验证
539             }
540             string birth = Id.Substring(6, 8).Insert(6, "-").Insert(4, "-");
541             DateTime time = new DateTime();
542             if (DateTime.TryParse(birth, out time) == false)
543             {
544                 return false;//生日验证
545             }
546             string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
547             string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
548             char[] Ai = Id.Remove(17).ToCharArray();
549             int sum = 0;
550             for (int i = 0; i < 17; i++)
551             {
552                 sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
553             }
554             int y = -1;
555             Math.DivRem(sum, 11, out y);
556             if (arrVarifyCode[y] != Id.Substring(17, 1).ToLower())
557             {
558                 return false;//校验码验证
559             }
560             return true;//符合GB11643-1999标准
561         }
562         public static bool IsIDCard15(string Id)
563         {
564             long n = 0;
565             if (long.TryParse(Id, out n) == false || n < Math.Pow(10, 14))
566             {
567                 return false;//数字验证
568             }
569             string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
570             if (address.IndexOf(Id.Remove(2)) == -1)
571             {
572                 return false;//省份验证
573             }
574             string birth = Id.Substring(6, 6).Insert(4, "-").Insert(2, "-");
575             DateTime time = new DateTime();
576             if (DateTime.TryParse(birth, out time) == false)
577             {
578                 return false;//生日验证
579             }
580             return true;//符合15位身份证标准
581         }
582         #endregion
583 
584         #region 是不是Int型的
585         /// <summary>
586         /// 是不是Int型的
587         /// </summary>
588         /// <param name="source"></param>
589         /// <returns></returns>
590         public static bool IsInt(string source)
591         {
592             Regex regex = new Regex(@"^(-){0,1}\d+$");
593             if (regex.Match(source).Success)
594             {
595                 if ((long.Parse(source) > 0x7fffffffL) || (long.Parse(source) < -2147483648L))
596                 {
597                     return false;
598                 }
599                 return true;
600             }
601             return false;
602         }
603         #endregion
604 
605         #region 看字符串的长度是不是在限定数之间 一个中文为两个字符
606         /// <summary>
607         /// 看字符串的长度是不是在限定数之间 一个中文为两个字符
608         /// </summary>
609         /// <param name="source">字符串</param>
610         /// <param name="begin">大于等于</param>
611         /// <param name="end">小于等于</param>
612         /// <returns></returns>
613         public static bool IsLengthStr(string source, int begin, int end)
614         {
615             int length = Regex.Replace(source, @"[^\x00-\xff]", "OK").Length;
616             if ((length <= begin) && (length >= end))
617             {
618                 return false;
619             }
620             return true;
621         }
622         #endregion
623 
624         #region 是不是中国电话,格式010-85849685
625         /// <summary>
626         /// 是不是中国电话,格式010-85849685
627         /// </summary>
628         /// <param name="source"></param>
629         /// <returns></returns>
630         public static bool IsTel(string source)
631         {
632             return Regex.IsMatch(source, @"^\d{3,4}-?\d{6,8}$", RegexOptions.IgnoreCase);
633         }
634         #endregion
635 
636         #region 邮政编码 6个数字
637         /// <summary>
638         /// 邮政编码 6个数字
639         /// </summary>
640         /// <param name="source"></param>
641         /// <returns></returns>
642         public static bool IsPostCode(string source)
643         {
644             return Regex.IsMatch(source, @"^\d{6}$", RegexOptions.IgnoreCase);
645         }
646         #endregion
647 
648         #region 中文
649         /// <summary>
650         /// 中文
651         /// </summary>
652         /// <param name="source"></param>
653         /// <returns></returns>
654         public static bool IsChinese(string source)
655         {
656             return Regex.IsMatch(source, @"^[\u4e00-\u9fa5]+$", RegexOptions.IgnoreCase);
657         }
658         public static bool hasChinese(string source)
659         {
660             return Regex.IsMatch(source, @"[\u4e00-\u9fa5]+", RegexOptions.IgnoreCase);
661         }
662         #endregion
663 
664         #region 验证是不是正常字符 字母,数字,下划线的组合
665         /// <summary>
666         /// 验证是不是正常字符 字母,数字,下划线的组合
667         /// </summary>
668         /// <param name="source"></param>
669         /// <returns></returns>
670         public static bool IsNormalChar(string source)
671         {
672             return Regex.IsMatch(source, @"[\w\d_]+", RegexOptions.IgnoreCase);
673         }
674         #endregion
675 
676         #region 验证用户名:必须以字母开头,可以包含字母、数字、“_”、“.”,至少5个字符
677         /// <summary>
678         /// 验证用户名:必须以字母开头,可以包含字母、数字、“_”、“.”,至少5个字符
679         /// </summary>
680         /// <param name="str"></param>
681         /// <returns></returns>
682         public static bool checkUserId(string str)
683         {
684             Regex regex = new Regex("[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}");
685             if (regex.Match(str).Success)
686                 if (regex.Matches(str)[0].Value.Length == str.Length)
687                     return true;
688             return false;
689         }
690         #endregion
691 
692         #region 验证是否为小数
693         static bool IsValidDecimal(string strIn)
694         {
695             return Regex.IsMatch(strIn, @"[0].d{1,2}|[1]");
696         }
697         #endregion
698 
699         #region 验证年月日
700         static bool IsValidDate(string strIn)
701         {
702             return Regex.IsMatch(strIn, @"^2d{3}-(?:0?[1-9]|1[0-2])-(?:0?[1-9]|[1-2]d|3[0-1])(?:0?[1-9]|1d|2[0-3]):(?:0?[1-9]|[1-5]d):(?:0?[1-9]|[1-5]d)$");
703         }
704         #endregion
705 
706         #region 验证日期格式
707         //检察是否正确的日期格式
708         public static bool IsDate(string str)
709         {
710             //考虑到了4年一度的366天,还有特殊的2月的日期
711             Regex reg = new Regex(@"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$");
712             return reg.IsMatch(str);
713         }
714         #endregion
715 
716         #region 验证后缀名
717         static  bool IsValidPostfix(string strIn)
718         {
719             return Regex.IsMatch(strIn, @".(?i:gif|jpg)$");
720         }
721         #endregion
722 
723         #region 验证字符是否在4至12之间
724         static bool IsValidByte(string strIn)
725         {
726             return Regex.IsMatch(strIn, @"^[a-z]{4,12}$");
727         }
728         #endregion
729          
730         #region 验证是否包含汉语/全部汉语
731         /// <summary>
732         /// 验证是否包含汉语
733         /// </summary>
734         /// <param name="str"></param>
735         /// <returns></returns>
736         public static bool IsHanyu(string str)
737         {
738             Regex regex = new Regex("[\u4e00-\u9fa5]");
739             if (regex.Match(str).Success)
740                 return true;
741             else
742                 return false;
743         }
744         /// <summary>
745         /// 验证是否全部汉语
746         /// </summary>
747         /// <param name="str"></param>
748         /// <returns></returns>
749         public static bool IsHanyuAll(string str)
750         {
751             Regex regex = new Regex("[\u4e00-\u9fa5]");
752             //匹配的内容长度和被验证的内容长度相同时,验证通过
753             if (regex.Match(str).Success)
754                 if (regex.Matches(str).Count == str.Length)
755                     return true;
756             //其它,未通过
757             return false;
758         }
759         #endregion
CheckHelper

2、实体

 1     /// <summary>
 2     /// 文件类型
 3     /// </summary>
 4     public enum EnumFileExtension
 5     {
 6         _7Z = 55122,
 7         ASPX = 239187,
 8         BMP = 6677,
 9         CHM = 7384,
10         CS = 117115,
11         DOC = 208207,
12         EXE = 7790,
13         GIF = 7173,
14         HTML = 6033,
15         JPG = 255216,
16         JS = 119105,
17         PDF = 3780,
18         PNG = 13780,
19         RAR = 8297,
20         SWF = 6787,
21         SQL = 255254,
22         TXT = 210187,
23         XLSX = 8075,
24         XLS = 208207,
25         XML = 6063,
26         ZIP = 8075,
27     }
View Code

猜你喜欢

转载自www.cnblogs.com/raniynight/p/9282583.html