smhy8187

 

C#验证类(使用正则表达式)

http://jieyulu.icode.csdn.net/post/2007/07/23/54694

C#验证类(使用正则表达式)

jieyulu | 23 七月, 2007 14:23 | C#

C#验证类(使用正则表达式)
  1. using System;    
  2. using System.Text.RegularExpressions;    
  3. namespace bobomousecom.crm    
  4. {    
  5.  /// <SUMMARY>    
  6.  /// Regexlib 的摘要说明。    
  7.  /// </SUMMARY>    
  8.  public class Regexlib    
  9.  {    
  10.   public Regexlib()    
  11.   {    
  12.    //    
  13.    // TODO: 在此处添加构造函数逻辑    
  14.    //    
  15.   }    
  16.   
  17.   //搜索输入字符串并返回所有 href=“...”值    
  18.   string DumpHrefs(String inputString)     
  19.   {    
  20.    Regex r;    
  21.    Match m;    
  22.    r = new Regex("href\s*=\s*(?:"(?<1>[^"]*)"|(?<1>\S+))",   
  23.     RegexOptions.IgnoreCase|RegexOptions.Compiled);   
  24.    for (m = r.Match(inputString); m.Success; m = m.NextMatch())    
  25.    {   
  26.     return("Found href " + m.Groups[1]);   
  27.    }   
  28.   }   
  29.  
  30.   //验证Email地址   
  31.   bool IsValidEmail(string strIn)   
  32.   {   
  33.    // Return true if strIn is in valid e-mail format.   
  34.    return Regex.IsMatch(strIn, @"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$");    
  35.   }   
  36.  
  37.   //dd-mm-yy 的日期形式代替 mm/dd/yy 的日期形式。   
  38.   string MDYToDMY(String input)    
  39.   {   
  40.    return Regex.Replace(input,"\b(?\d{1,2})/(?\d{1,2})/(?\d{2,4})\b","${day}-${month}-${year}");   
  41.   }   
  42.  
  43.   //验证是否为小数   
  44.   bool IsValidDecimal(string strIn)   
  45.   {   
  46.       
  47.    return Regex.IsMatch(strIn,@"[0].d{1,2}|[1]");    
  48.   }   
  49.  
  50.   //验证是否为电话号码   
  51.   bool IsValidTel(string strIn)   
  52.   {   
  53.    return Regex.IsMatch(strIn,@"(d+-)?(d{4}-?d{7}|d{3}-?d{8}|^d{7,8})(-d+)?");    
  54.   }   
  55.  
  56.   //验证年月日   
  57.   bool IsValidDate(string strIn)   
  58.   {   
  59.    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)$");    
  60.   }   
  61.  
  62.   //验证后缀名   
  63.   bool IsValidPostfix(string strIn)   
  64.   {   
  65.    return Regex.IsMatch(strIn,@".(?i:gif|jpg)$");    
  66.   }   
  67.  
  68.   //验证字符是否在4至12之间   
  69.   bool IsValidByte(string strIn)   
  70.   {   
  71.    return Regex.IsMatch(strIn,@"^[a-z]{4,12}$");    
  72.   }   
  73.  
  74.   //验证IP   
  75.   bool IsValidIp(string strIn)   
  76.   {   
  77.    return Regex.IsMatch(strIn,@"^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$");     
  78.   }     
  79.  }    
  80. }  

posted on 2007-07-31 23:34  new2008  阅读(291)  评论(0编辑  收藏  举报

导航