只能輸入1個數字 
表達式 ^/d$
描述 匹配一個數字
匹配的例子 0,1,2,3
不匹配的例子  

 

只能輸入n個數字  
表達式 ^/d{n}$ 例如^/d{8}$
描述 匹配8個數字
匹配的例子 12345678,22223334,12344321
不匹配的例子  

 

只能輸入至少n個數字 
表達式 ^/d{n,}$ 例如^/d{8,}$
描述 匹配最少n個數字
匹配的例子 12345678,123456789,12344321
不匹配的例子  

 

只能輸入m到n個數字 
表達式 ^/d{m,n}$ 例如^/d{7,8}$
描述 匹配m到n個數字
匹配的例子 12345678,1234567
不匹配的例子 123456,123456789

 

只能輸入數字  
表達式 ^[0-9]*$
描述 匹配任意個數字
匹配的例子 12345678,1234567
不匹配的例子 E,

 

只能輸入某個區間數字  
表達式 ^[12-15]$
描述 匹配某個區間的數字
匹配的例子 12,13,14,15
不匹配的例子  

 

只能輸入0和非0打頭的數字  
表達式 ^(0|[1-9][0-9]*)$
描述 可以為0,第一個數字不能為0,數字中可以有0
匹配的例子 12,10,101,100
不匹配的例子 01,

 

只能輸入實數  
表達式 ^[-+]?/d+(/./d+)?$
描述 匹配實數
匹配的例子 18,+3.14,-9.90
不匹配的例子 .6,33s,67-99

 

只能輸入n位小數的正實數  
表達式 ^[0-9]+(.[0-9]{n})?$以^[0-9]+(.[0-9]{2})?$為例
描述 匹配n位小數的正實數
匹配的例子 2.22
不匹配的例子 2.222,-2.22,

 

只能輸入mn位小數的正實數  
表達式 ^[0-9]+(.[0-9]{m,n})?$以^[0-9]+(.[0-9]{1,2})?$為例
描述 匹配m到n位小數的正實數
匹配的例子 2.22,2.2
不匹配的例子 2.222,-2.2222,

 

只能輸入非0的正整數  
表達式 ^/+?[1-9][0-9]*$
描述 匹配非0的正整數
匹配的例子 2,23,234
不匹配的例子 0,-4,

 

只能輸入非0的負整數 
表達式 ^/-[1-9][0-9]*$
描述 匹配非0的負整數
匹配的例子 -2,-23,-234
不匹配的例子 0,4,

 

只能輸入n個字符 
表達式 ^.{n}$ 以^.{4}$為例
描述 匹配n個字符,注意漢字只算1個字符
匹配的例子 1234,12we,123清,清清月兒
不匹配的例子 0,123,123www,

 

只能輸入英文字符 
表達式 ^.[A-Za-z]+$為例
描述 匹配英文字符,大小寫任意
匹配的例子 Asp,WWW,
不匹配的例子 0,123,123www,

 

只能輸入大寫英文字符 
表達式 ^.[AZ]+$為例
描述 匹配英文大寫字符
匹配的例子 NET,WWW,
不匹配的例子 0,123,123www,

 

只能輸入小寫英文字符 
表達式 ^.[az]+$為例
描述 匹配英文大寫字符
匹配的例子 asp,csdn
不匹配的例子 0,NET,WWW,

 

只能輸入英文字符+數字 
表達式 ^.[A-Za-z0-9]+$為例
描述 匹配英文字符+數字
匹配的例子 1Asp,W1W1W,
不匹配的例子 0,123,123,www,

 

只能輸入英文字符/數字/下劃線 
表達式 ^/w+$為例
描述 匹配英文字符或數字或下劃線
匹配的例子 1Asp,WWW,12,1_w
不匹配的例子 3#,2-4,w#$,

 

密碼舉例 
表達式 ^.[a-zA-Z] /w{m,n}$
描述 匹配英文字符開頭的mn位字符且只能數字字母或下劃線
匹配的例子  
不匹配的例子  

 

驗證首字母大寫 
表達式 /b[^/Wa-z0-9_][^/WA-Z0-9_]*/b
描述 首字母只能大寫
匹配的例子 Asp,Net
不匹配的例子  

 

驗證網址(帶?id=中文)VS.NET2005無此功能 
表達式

^http:////([/w-]+(/.[/w-]+)+(//[/w- .///?%&=/u4e00-/u9fa5]*)?) ?$

描述 驗證帶?id=中文
匹配的例子
http://blog.csdn.net?id=清清月兒
不匹配的例子  

 

驗證漢字 
表達式 ^[/u4e00-/u9fa5]{0,}$
描述 只能漢字
匹配的例子 清清月兒
不匹配的例子  

 

驗證QQ號 
表達式 [0-9]{5,9}
描述 5-9位的QQ號
匹配的例子 10000,123456
不匹配的例子 10000w,

 

驗證電子郵件(驗證MSN號一樣)
表達式 /w+([-+.´]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*
描述 注意MSN用非hotmail.com郵箱也可以
匹配的例子 aaa@msn.com
不匹配的例子 111@1.

 

驗證身份證號(粗驗,最好服務器端調類庫再細驗證) 
表達式 ^[1-9]([0-9]{16}|[0-9]{13})[xX0-9]$
描述  
匹配的例子 15或者18位的身份證號,支持帶X的
不匹配的例子  

 

驗證手機號(包含159,不包含小靈通) 
表達式 ^13[0-9]{1}[0-9]{8}|^15[9]{1}[0-9]{8}
描述 包含159的手機號130-139
匹配的例子 139XXXXXXXX
不匹配的例子 140XXXXXXXX,

 

驗證電話號碼號(很複雜,VS.NET2005給的是錯的)
表達式(不完美 方案一((/(/d{3}/)|/d{3}-)|(/(/d{4}/)|/d{4}-))?(/d{8}|/ d{7}) 
方案二(^[0-9]{3,4}/-[0-9]{3,8}$)|(^[0-9]{3,8}$)|( ^/([0-9]{3,4}/)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$) 支持手機號但也不完美
描述

上海:02112345678 3+8位
上海:021-12345678 
上海:(021)-12345678 
上海:(021)12345678 
鄭州:03711234567 4+7位
杭州:057112345678 4+8位
還有帶上分機號,國家碼的情況由於情況非常複雜所以不建議前台做100%驗證,到目前為止似乎也沒有誰能寫一個包含所有的類型 如果誰有更好 的驗證電話的請留言,其實有很多情況本身就是矛盾的。

匹配的例子  
不匹配的例子  

 

驗證護照 
表達式

(P/d{7})|G/d{8})

描述 驗證P+7個數字和G+8個數字
匹配的例子  
不匹配的例子  

 

驗證IP 
表達式

^(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] )$

描述 驗證IP
匹配的例子 192.168.0.1 222.234.1.4
不匹配的例子  

 

驗證域
表達式

^[a-zA-Z0-9]+([a-zA-Z0-9/-/.]+)?/.(com|org|net|cn|com.cn|edu.cn|grv.cn |)$

描述 驗證域
匹配的例子 csdn.net baidu.com it.com.cn
不匹配的例子 192.168.0.1

 

驗證信用卡
表達式

^((?:4/d{3})|(?:5[1-5]/d{2})|(?:6011)|(?:3[68]/d{2})|( ?:30[012345]/d))[ -]?(/d{4})[ -]?(/d{4})[ -]?(/d{4}|3[4,7]/ d{13})$

描述 驗證VISA卡,萬事達卡,Discover卡,美國運通卡
匹配的例子  
不匹配的例子  

 

驗證ISBN 國際標準書號
表達式

^(/d[- ]*){9}[/dxX]$

描述 驗證ISBN國際標準書號
匹配的例子 7-111-19947-2
不匹配的例子  

 

驗證GUID 全球唯一標識符
表達式

^[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0- 9]{12}$

描述 格式8-4-4-4-12
匹配的例子 2064d355-c0b9-41d8-9ef7-9d8b26524751
不匹配的例子  

 

驗證文件路徑和擴展名
表達式

^([a-zA-Z]/:|//)//([^//]+//)*[^//:*?"<>|]+/.txt(l)?$

描述 檢查路徑和文件擴展名
匹配的例子 E:/mo.txt
不匹配的例子 E:/ , mo.doc, E:/mo.doc ,

 

驗證Html顏色值
表達式

^#?([af]|[AF]|[0-9]){3}(([af]|[AF]|[0-9]){3})?$

描述 檢查顏色取值
匹配的例子 #FF0000
不匹配的例子  

原出處:

http://blog.csdn.net/yoland/article/details/4731863

using System;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
namespace Legalsoft.Wizard.Basic // 設置專案屬性可修改本專案的命名空間
{
	///
	///頁面資料校驗類
	///修改自 李天平先生的作品,一併感謝。
	/// 
	public class PageValidate
	{
		private static Regex RegNumber = new Regex("^[0-9]+$");
		private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
		private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
		//等價於^[+-]?\d+[.]?\d+$
		private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); 
		//w 英文字母或數位的字串,和 [a-zA-Z0-9] 語法一樣 
		private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");
		private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");
		public PageValidate()
		{
		}
		#region 數位字串檢查		
		/// 
		/// 檢查Request查詢字串的鍵值,是否是數字,最大長度限制
		/// 
		/// Request
		/// Request的鍵值
		/// 最大長度
		/// 返回Request查詢字串
		public static string IsDigit(HttpRequest req, string inputKey, int maxLen)
		{
			string retVal = string.Empty;
			if(inputKey != null && inputKey != string.Empty)
			{
				retVal = req.QueryString[inputKey];
				if(null == retVal)
					retVal = req.Form[inputKey];
				if(null != retVal)
				{
					retVal = SqlText(retVal, maxLen);
					if(!IsNumber(retVal))
						retVal = string.Empty;
				}
			}
			if(retVal == null)
				retVal = string.Empty;
			return retVal;
		}
		/// 
		/// 是否數位字串
		/// 
		/// 輸入字串
		/// 
		public static bool IsNumber(string inputData)
		{
			Match m = RegNumber.Match(inputData);
			return m.Success;
		}
		/// 
		/// 是否數位字串 可帶正負號
		/// 
		/// 輸入字串
		/// 
		public static bool IsNumberSign(string inputData)
		{
			Match m = RegNumberSign.Match(inputData);
			return m.Success;
		}
		/// 
		/// 是否是浮點數
		/// 
		/// 輸入字串
		/// 
		public static bool IsDecimal(string inputData)
		{
			Match m = RegDecimal.Match(inputData);
			return m.Success;
		}
		/// 
		/// 是否是浮點數 可帶正負號
		/// 
		/// 輸入字串
		/// 
		public static bool IsDecimalSign(string inputData)
		{
			Match m = RegDecimalSign.Match(inputData);
			return m.Success;
		}		
		#endregion
		#region 中文檢測
		/// 
		/// 檢測是否有中文字元
		/// 
		/// 
		/// 
		public static bool IsHasCHZN(string inputData)
		{
			Match m = RegCHZN.Match(inputData);
			return m.Success;
		}	
		#endregion
		#region 郵寄地址
		/// 
		/// 是否是浮點數 可帶正負號
		/// 
		/// 輸入字串
		/// 
		public static bool IsEmail(string inputData)
		{
			Match m = RegEmail.Match(inputData);
			return m.Success;
		}		
		#endregion
		#region 其他
		/// 
		/// 檢查字串最大長度,返回指定長度的串
		/// 
		/// 輸入字串
		/// 最大長度
		/// 			
		public static string SqlText(string sqlInput, int maxLength)
		{			
			if(sqlInput != null && sqlInput != string.Empty)
			{
				sqlInput = sqlInput.Trim();							
				if(sqlInput.Length > maxLength)//按最大長度截取字串
					sqlInput = sqlInput.Substring(0, maxLength);
			}
			return sqlInput;
		}
		/// 
		/// 字串編碼
		/// 
		/// 
		/// 
		public static string HtmlEncode(string inputData)
		{
			return HttpUtility.HtmlEncode(inputData);
		}
		/// 
		/// 設置Label顯示Encode的字串
		/// 
		/// 
		/// 
		public static void SetLabel(Label lbl, string txtInput)
		{
			lbl.Text = HtmlEncode(txtInput);
		}
		public static void SetLabel(Label lbl, object inputObj)
		{
			SetLabel(lbl, inputObj.ToString());
		}		
		#endregion
	}
}

原出處:

http://www.legalsoft.com.cn/docs/980.html

arrow
arrow
    文章標籤
    C# Regex RegularExpression
    全站熱搜

    Jimmy 發表在 痞客邦 留言(0) 人氣()