重慶分公司,新征程啟航
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊、服務(wù)器等服務(wù)
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊、服務(wù)器等服務(wù)
這篇文章主要介紹.Net項目中常用驗證操作有哪些,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
在項目中需要對用戶輸入的信息,以及一些方法生成的結(jié)果進行驗證,一般在項目中較多的采用js插件或js來進行有關(guān)信息的校驗,但是從項目安全性的角度進行考慮,可對系統(tǒng)進行js注入。
如果在后臺對用戶輸入的信息進行驗證會相對的安全,在出現(xiàn)信息驗證不合法時,可以直接在程序中拋出異常,終止程序的運行。
現(xiàn)在提供幾種較為常用的驗證方法,可以減少在項目中開發(fā)時間和錯誤性:
1.判斷域名:
////// 普通的域名 /// /// ///public static bool IsCommonDomain(string value) { return QuickValidate("^(www.)?(\\w+\\.){1,3}(org|org.cn|gov.cn|com|cn|net|cc)$", value.ToLower()); }
2.檢查一個字符串是否是純數(shù)字構(gòu)成的,一般用于查詢字符串參數(shù)的有效性驗證:
////// 檢查一個字符串是否是純數(shù)字構(gòu)成的,一般用于查詢字符串參數(shù)的有效性驗證。 /// /// 需驗證的字符串。 ///是否合法的bool值。 public static bool IsNumeric(string value) { return QuickValidate("^[-]?[1-9]*[0-9]*$", value); }
3.檢查一個字符串是否是純字母和數(shù)字構(gòu)成的,一般用于查詢字符串參數(shù)的有效性驗證:
////// 檢查一個字符串是否是純字母和數(shù)字構(gòu)成的,一般用于查詢字符串參數(shù)的有效性驗證。 /// /// 需驗證的字符串。 ///是否合法的bool值。 public static bool IsLetterOrNumber(string value) { return QuickValidate("^[a-zA-Z0-9_]*$", value); }
4.判斷是否是數(shù)字,包括小數(shù)和整數(shù):
////// 判斷是否是數(shù)字,包括小數(shù)和整數(shù)。 /// /// 需驗證的字符串。 ///是否合法的bool值。 public static bool IsNumber(string value) { return QuickValidate("^(0|([1-9]+[0-9]*))(.[0-9]+)?$", value); }
5.快速驗證一個字符串是否符合指定的正則表達式:
////// 快速驗證一個字符串是否符合指定的正則表達式。 /// /// 正則表達式的內(nèi)容。 /// 需驗證的字符串。 ///是否合法的bool值。 public static bool QuickValidate(string express, string value) { var myRegex = new System.Text.RegularExpressions.Regex(express); return value.Length != 0 && myRegex.IsMatch(value); }
6.判斷一個字符串是否為郵件:
////// 判斷一個字符串是否為郵件 /// /// ///public static bool IsEmail(string value) { var regex = new System.Text.RegularExpressions.Regex(@"^\w+([-+.]\w+)*@(\w+([-.]\w+)*\.)+([a-zA-Z]+)+$", RegexOptions.IgnoreCase); return regex.Match(value).Success; }
7.判斷一個字符串是否為郵編:
////// 判斷一個字符串是否為郵編 /// /// ///public static bool IsZipCode(string value) { return QuickValidate("^([0-9]{6})$", value); }
8.判斷一個字符串是否為ID格式:
////// 判斷一個字符串是否為ID格式 /// /// ///public static bool IsIdCard(string value) { System.Text.RegularExpressions.Regex regex; string[] strArray; if ((value.Length != 15) && (value.Length != 0x12)) { return false; } if (value.Length == 15) { regex = new System.Text.RegularExpressions.Regex(@"^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$"); if (!regex.Match(value).Success) { return false; } strArray = regex.Split(value); try { var dateTime = new DateTime(int.Parse("19" + strArray[2]), int.Parse(strArray[3]), int.Parse(strArray[4])); return true; } catch { return false; } } regex = new System.Text.RegularExpressions.Regex(@"^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9Xx])$"); if (!regex.Match(value).Success) { return false; } strArray = regex.Split(value); try { var dateTime = new DateTime(int.Parse(strArray[2]), int.Parse(strArray[3]), int.Parse(strArray[4])); return true; } catch { return false; } }
9.判斷是不是純中文:
////// 判斷是不是純中文 /// /// ///public static bool IsChinese(string value) { var regex = new System.Text.RegularExpressions.Regex(@"^[\u4E00-\u9FA5\uF900-\uFA2D]+$", RegexOptions.IgnoreCase); return regex.Match(value).Success; }
10.判斷一個字符串是否為手機號碼:
////// 判斷一個字符串是否為手機號碼 /// /// ///public static bool IsMobileNum(string value) { var regex = new System.Text.RegularExpressions.Regex(@"^(13|15)\d{9}$", RegexOptions.IgnoreCase); return regex.Match(value).Success; }
11.判斷一個字符串是否為電話號碼:
////// 判斷一個字符串是否為電話號碼 /// /// ///public static bool IsPhoneNum(string value) { var regex = new System.Text.RegularExpressions.Regex(@"^(86)?(-)?(0\d{2,3})?(-)?(\d{7,8})(-)?(\d{3,5})?$", RegexOptions.IgnoreCase); return regex.Match(value).Success; }
12.判斷一個字符串是否為網(wǎng)址:
////// 判斷一個字符串是否為網(wǎng)址 /// /// ///public static bool IsUrl(string value) { var regex = new System.Text.RegularExpressions.Regex(@"(http://)?([\w-]+\.)*[\w-]+(/[\w- ./?%&=]*)?", RegexOptions.IgnoreCase); return regex.Match(value).Success; }
13.判斷一個字符串是否為IP地址:
////// 判斷一個字符串是否為IP地址 /// /// ///public static bool IsIp(string value) { var regex = new System.Text.RegularExpressions.Regex(@"^(((2[0-4]{1}[0-9]{1})|(25[0-5]{1}))|(1[0-9]{2})|([1-9]{1}[0-9]{1})|([0-9]{1})).(((2[0-4]{1}[0-9]{1})|(25[0-5]{1}))|(1[0-9]{2})|([1-9]{1}[0-9]{1})|([0-9]{1})).(((2[0-4]{1}[0-9]{1})|(25[0-5]{1}))|(1[0-9]{2})|([1-9]{1}[0-9]{1})|([0-9]{1})).(((2[0-4]{1}[0-9]{1})|(25[0-5]{1}))|(1[0-9]{2})|([1-9]{1}[0-9]{1})|([0-9]{1}))$", RegexOptions.IgnoreCase); return regex.Match(value).Success; }
14.判斷一個字符串是否為字母加數(shù)字:
////// 判斷一個字符串是否為字母加數(shù)字 /// Regex("[a-zA-Z0-9]?" /// /// ///public static bool IsWordAndNum(string value) { var regex = new System.Text.RegularExpressions.Regex("[a-zA-Z0-9]?"); return regex.Match(value).Success; }
以上是“.Net項目中常用驗證操作有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!