This article describes the C# class that implements verification of user input data. Share it for your reference. The details are as follows:
This C# class contains various commonly used data verification functions, including verifying whether it is a number, verifying the email format, distinguishing between Chinese and English intercepting strings, distinguishing between Chinese and English calculating string lengths, detecting whether it contains Chinese characters, determining whether it contains specific characters, etc.
using System; using ; using ; using ; using ; namespace { 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]+$"); private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //Equivalent to ^[+-]?\d+[.]?\d+$ private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");//w English letters or numbers strings, the same syntax as [a-zA-Z0-9] private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]"); public PageValidate() { } #region Number string check /// <summary> /// Format string /// </summary> /// <param name="inputData">Source String</param> /// <param name="formatlevel">0:No verification| 1:sql statement parameters| 2:stored procedure parameters| 3:EncodeHtml| 4:Encode+sql| 5:Encode+sql| /// <returns>Returns the formatted string</returns> public static string FormatString(string inputData, int formatlevel) { return inputData; } /// <summary> /// Check the key value of the Request query string, whether it is a number, and the maximum length limit /// </summary> /// <param name="req">Request</param> /// <param name="inputKey">Request's key value</param> /// <param name="maxLen">Maximum length</param> /// <returns>Returns the Request query string</returns> public static string FetchInputDigit(HttpRequest req, string inputKey, int maxLen) { string retVal = ; if (inputKey != null && inputKey != ) { retVal = [inputKey]; if (null == retVal) retVal = [inputKey]; if (null != retVal) { retVal = SqlText(retVal, maxLen); if (!IsNumber(retVal)) retVal = ; } } if (retVal == null) retVal = ; return retVal; } public enum CheckType { None, Int, SignInt, Float, SignFloat, Chinese, Mail } /// <summary> /// Detect string type /// </summary> /// <param name="inputData">Input string</param> /// <param name="checktype">0:Not detected| 1:number| 2:symbol number| 3:float number| 4:synoplasty| 5:Chinese?| 6:mail?</param> /// <returns></returns> public static bool checkString(string inputData, int checktype) { bool _return = false; switch (checktype) { case 0: _return = true; break; case 1: _return = IsNumber(inputData); break; case 2: _return = IsNumberSign(inputData); break; case 3: _return = IsDecimal(inputData); break; case 4: _return = IsDecimalSign(inputData); break; case 5: _return = IsHasCHZN(inputData); break; case 6: _return = IsEmail(inputData); break; default: _return = false; break; } return _return; } /// <summary> /// Whether to numeric string /// </summary> /// <param name="inputData">Input string</param> /// <returns></returns> public static bool IsNumber(string inputData) { Match m = (inputData); return ; } /// <summary> /// Whether the numeric string can be with positive and negative signs /// </summary> /// <param name="inputData">Input string</param> /// <returns></returns> public static bool IsNumberSign(string inputData) { Match m = (inputData); return ; } /// <summary> /// Is it a floating point number /// </summary> /// <param name="inputData">Input string</param> /// <returns></returns> public static bool IsDecimal(string inputData) { Match m = (inputData); return ; } /// <summary> /// Is it a floating point number? It can have positive and negative signs /// </summary> /// <param name="inputData">Input string</param> /// <returns></returns> public static bool IsDecimalSign(string inputData) { Match m = (inputData); return ; } #endregion #region Chinese test /// <summary> /// Check whether there are Chinese characters /// </summary> /// <param name="inputData"></param> /// <returns></returns> public static bool IsHasCHZN(string inputData) { Match m = (inputData); return ; } #endregion public static string GetShortDate(string dt) { return (dt).ToShortDateString(); } #region Email Address /// <summary> /// Is it a floating point number? It can have positive and negative signs /// </summary> /// <param name="inputData">Input string</param> /// <returns></returns> public static bool IsEmail(string inputData) { Match m = (inputData); return ; } #endregion #region Other /// <summary> /// Check the maximum length of the string and return the string of the specified length /// </summary> /// <param name="sqlInput">Input string</param> /// <param name="maxLength">Maximum length</param> /// <returns></returns> public static string SqlText(string sqlInput, int maxLength) { if (sqlInput != null && sqlInput != ) { sqlInput = (); if ( > maxLength)//Seave string by maximum length sqlInput = (0, maxLength); } return sqlInput; } /// <summary> /// String encoding /// </summary> /// <param name="inputData"></param> /// <returns></returns> public static string HtmlEncode(string inputData) { return (inputData); } /// <summary> /// Set Label to display Encode string /// </summary> /// <param name="lbl"></param> /// <param name="txtInput"></param> public static void SetLabel(Label lbl, string txtInput) { = HtmlEncode(txtInput); } public static void SetLabel(Label lbl, object inputObj) { SetLabel(lbl, ()); } #endregion #region Decryption process for user permissions read from the database public static string switch_riddle(string s_ch)//Decryption { string s_out, s_temp, temp; int i_len = s_ch.Length; if (i_len == 0 || s_ch == "") { s_out = "0"; } temp = ""; s_temp = ""; s_out = ""; for (int i = 0; i <= i_len - 1; i++) { temp = s_ch.Substring(i, 1); switch (temp) { case "a": s_temp = "1010"; break; case "b": s_temp = "1011"; break; case "c": s_temp = "1100"; break; case "d": s_temp = "1101"; break; case "e": s_temp = "1110"; break; case "f": s_temp = "1111"; break; case "0": s_temp = "0000"; break; case "1": s_temp = "0001"; break; case "2": s_temp = "0010"; break; case "3": s_temp = "0011"; break; case "4": s_temp = "0100"; break; case "5": s_temp = "0101"; break; case "6": s_temp = "0110"; break; case "7": s_temp = "0111"; break; case "8": s_temp = "1000"; break; case "9": s_temp = "1001"; break; default: s_temp = "0000"; break; } s_out = s_out + s_temp; s_temp = ""; } return s_out; } #endregion #region Encryption process of user permissions public static string switch_encrypt(string s_ch) { string s_out, s_temp, temp; int i_len = 64; if (i_len == 0 || s_ch == "") { s_out = "0000"; } temp = ""; s_temp = ""; s_out = ""; for (int i = 0; i <= i_len - 1; i = i + 4) { temp = s_ch.Substring(i, 4); switch (temp) { case "1010": s_temp = "a"; break; case "1011": s_temp = "b"; break; case "1100": s_temp = "c"; break; case "1101": s_temp = "d"; break; case "1110": s_temp = "e"; break; case "1111": s_temp = "f"; break; case "0000": s_temp = "0"; break; case "0001": s_temp = "1"; break; case "0010": s_temp = "2"; break; case "0011": s_temp = "3"; break; case "0100": s_temp = "4"; break; case "0101": s_temp = "5"; break; case "0110": s_temp = "6"; break; case "0111": s_temp = "7"; break; case "1000": s_temp = "8"; break; case "1001": s_temp = "9"; break; default: s_temp = "0"; break; } s_out = s_out + s_temp; s_temp = ""; } return s_out; }//encryption #endregion #region Access public static bool CheckTrue(string s_admin, int a) { string s_temp = ""; s_temp = s_admin.Substring(a - 1, 1); //s_admin is a global variable if (s_temp == "" || s_temp == "1") { return true; } else { return false; } } #endregion #region Detection of string length /// <summary> /// Calculate the text length and distinguish between Chinese and English characters. Chinese calculates two lengths and English calculates one length /// </summary> /// <param name="Text">Stand that needs to be calculated in length</param> /// <returns>int</returns> public static int Text_Length(string Text) { int len = 0; for (int i = 0; i < ; i++) { byte[] byte_len = ((i, 1)); if (byte_len.Length > 1) len += 2; //If the length is greater than 1, it is Chinese, accounting for two bytes, +2 else len += 1; //If the length is equal to 1, it is in English, occupying one byte, +1 } return len; } #endregion #region string length distinction between Chinese and English intercept /// <summary> /// Intercept the text and distinguish Chinese and English characters. Chinese counts two lengths and English counts one length /// </summary> /// <param name="str">String to be intercepted</param> /// <param name="length">Stand that needs to be calculated in length</param> /// <returns>string</returns> public static string GetSubString(string str, int length) { string temp = str; int j = 0; int k = 0; for (int i = 0; i < ; i++) { if (((i, 1), @"[\u4e00-\u9fa5]+")) { j += 2; } else { j += 1; } if (j <= length) { k += 1; } if (j > length) { return (0, k) + ".."; } } return temp; } #endregion #region Page HTML Format public static string GetHtml(string sDetail) { Regex r; Match m; #region Process spaces sDetail = (" ", "&nbsp;"); #endregion #region Handle single quotes sDetail = ("'", "'"); #endregion #region handles double quotes sDetail = ("\"", "&quot;"); #endregion #region html tag sDetail = ("<", "&lt;"); sDetail = (">", "&gt;"); #endregion #region Handle line breaks // Process newlines and add two full-width spaces before each newline r = new Regex(@"(\r\n((&nbsp;)| )+)(?<text>\S+)", ); for (m = (sDetail); ; m = ()) { sDetail = ([0].ToString(), "<BR>" + ["text"].ToString()); } // Process newlines and add two full-width spaces before each newline sDetail = ("\r\n", "<BR>"); #endregion return sDetail; } #endregion #region Pagination //public static string paging(string url, string para, int sumpage, int page) //{ // string result = ; // if (sumpage == 1) // { // return result; // } // if (page > sumpage) // { // page = 1; // } // if (sumpage > 0) // { // for (int i = 1; i <= sumpage; i++) // { // if (i == page) // { // result += ("<a class=\"a_page\" href=\"{0}?page={1}{2}\">{3}</a> ", new object[] { url, (), para, () }); // } // else // { // result += ("<a href=\"{0}?page={1}{2}\">{3}</a> ", new object[] { url, (), para, () }); // } // } // } // return result; //} public static string paging(string url, string para, int sumpage, int page) { string result = ; if (sumpage == 1) { return result; } if (sumpage > 500) { sumpage = 500; } if (page > sumpage) { page = 1; } StringBuilder sb = new StringBuilder(); if (sumpage > 0) { switch (page) { case 1: (("<p class=\"next\"><a href=\"{0}?page={1}{2}\">{3}</a> ", new object[] { url, page + 1, para, "Next Page" })); break; default: if (sumpage == page) { (("<p class=\"next\"><a href=\"{0}?page={1}{2}\">{3}</a> ", new object[] { url, page - 1, para, "Previous Page" })); } else { (("<p class=\"next\"><a href=\"{0}?page={1}{2}\">{3}</a> <a href=\"{4}?page={5}{6}\">{7}</a> ", new object[] { url, page + 1, para, "Next Page", url, page - 1, para, "Previous Page" })); } break; } (("The{0}/{1}Page</p>", new object[] { page, sumpage })); } return (); } public static string paging(string url, string para, int sumpage, int page, myPaging) { = false; string result = ; if (sumpage == 1) { return result; } if (sumpage > 500) { sumpage = 500; } if (page > sumpage) { page = 1; } StringBuilder sb = new StringBuilder(); if (sumpage > 0) { = true; switch (page) { case 1: (("<a href=\"{0}?page={1}{2}\">{3}</a> ", new object[] { url, page + 1, para, "Next Page" })); break; default: if (sumpage == page) { (("<a href=\"{0}?page={1}{2}\">{3}</a> ", new object[] { url, page - 1, para, "Previous Page" })); } else { (("<a href=\"{0}?page={1}{2}\">{3}</a> <a href=\"{4}?page={5}{6}\">{7}</a> ", new object[] { url, page + 1, para, "Next Page", url, page - 1, para, "Previous Page" })); } break; } (("The{0}/{1}Page", new object[] { page, sumpage })); } return (); } public static string paging(string para, int sumpage, int page, int count) { string result = ; if (page > sumpage) { page = 1; } StringBuilder sb = new StringBuilder(); if (sumpage > 0) { if (sumpage != 1) { switch (page) { case 1: (("<a href=\"?page={0}{1}\">{2}</a> ", new object[] { page + 1, para, "Next Page" })); break; default: if (sumpage == page) { (("<a href=\"?page={0}{1}\">{2}</a> ", new object[] { page - 1, para, "Previous Page" })); } else { (("<a href=\"?page={0}{1}\">{2}</a> <a href=\"?page={3}{4}\">{5}</a> ", new object[] { page - 1, para, "Previous Page", page + 1, para, "Next Page" })); } break; } } (("The{0}/{1}Page common{2}strip", new object[] { page, sumpage, count })); } return (); } public static void paging(string clinktail, int sumpage, int page, page_view) { if (sumpage > 0) { int n = sumpage; //Total page count int x = page; //Get the current page int i; int endpage; string pageview = "", pageviewtop = ""; if (x > 1) { pageview += "&nbsp;&nbsp;<a class='pl' href='?page=1" + clinktail + "'>The1Page</a> | "; pageviewtop += "&nbsp;&nbsp;<a class='pl' href='?page=1" + clinktail + "'>The1Page</a> | "; } else { pageview += "&nbsp;&nbsp;<font color='#666666'> Page 1 </font> | "; pageviewtop += "&nbsp;&nbsp;<font color='#666666'> Page 1 </font> | "; } if (x > 1) { pageviewtop += " <a class='pl' href='?page=" + (x - 1) + "" + clinktail + "'>superior1Page</a> "; } else { pageviewtop += " <font color='#666666'>Previous 1 page</font> "; } if (x > ((x - 1) / 10) * 10 && x > 10) { pageview += "<a class='pl' href='?page=" + ((x - 1) / 10) * 10 + "" + clinktail + "' onclink='return false;'>superior10Page</a>"; } //if (((x-1) / 10) * 10 + 10) >= n ) if (((x - 1) / 10) * 10 + 10 >= n) { endpage = n; } else { endpage = ((x - 1) / 10) * 10 + 10; } for (i = ((x - 1) / 10) * 10 + 1; i <= endpage; ++i) { if (i == x) { pageview += " <font color='#FF0000'><b>" + i + "</b></font>"; } else { pageview += " <a class='pl' href='?page=" + i + "" + clinktail + "'>" + i + "</a>"; } } if (x < n) { pageviewtop += " <a class='pl' href='?page=" + (x + 1) + "" + clinktail + "'>Down1Page</a> "; } else { pageviewtop += " <font color='#666666'>Next 1 page</font> "; } if (endpage != n) { pageview += " <a class='pl' href='?page=" + (endpage + 1) + "" + clinktail + "' class='pl' onclink='return false;'>Down10Page</a> | "; } else { pageview += " | "; } if (x < n) { pageview += " <a class='pl' href='?page=" + n + "" + clinktail + "' class='pl'>The" + n + "Page</a> "; pageviewtop += " | <a class='pl' href='?page=" + n + "" + clinktail + "' class='pl'>The" + n + "Page</a> "; } else { pageview += "<font color='#666666'> " + n + "page </font>"; pageviewtop += " | <font color='#666666'> " + n + "page </font>"; } page_view.Text = (); } else { page_view.Text = ""; } } //With the first and last page public static string paging2(string para, int sumpage, int page, int count) { string result = ; if (page > sumpage) { page = 1; } StringBuilder sb = new StringBuilder(); if (sumpage > 0) { if (sumpage != 1) { //Page 1 (("<a href=\"?page={0}{1}\"><img src=\"images/\" border=\"0\"/></a>&nbsp;&nbsp;", new object[] { 1, para })); switch (page) { case 1: //Picture on the previous page (("<a>{0}</a>", new object[] { "<img src=\"images/\" border=\"0\"/>" })); (("<a>superior一Page</a><a href=\"?page={0}{1}\">{2}</a> ", new object[] { page + 1, para, "Next Page" })); //Pictures on the next page (("<a href=\"?page={0}{1}\">{2}</a>", new object[] { page + 1, para, "<img src=\"images/\" border=\"0\"/>" })); break; default: if (sumpage == page) { //Picture on the previous page (("<a href=\"?page={0}{1}\">{2}</a>", new object[] { page - 1, para, "<img src=\"images/\" border=\"0\"/>" })); (("<a href=\"?page={0}{1}\">{2}</a><a>Down一Page</a> ", new object[] { page - 1, para, "Previous Page" })); //Pictures on the next page (("<a>{0}</a>", new object[] { "<img src=\"images/\" />" })); } else { //Picture on the previous page (("<a href=\"?page={0}{1}\">{2}</a>", new object[] { page - 1, para, "<img src=\"images/\" border=\"0\"/>" })); (("<a href=\"?page={0}{1}\">{2}</a> <a href=\"?page={3}{4}\">{5}</a> ", new object[] { page - 1, para, "Previous Page", page + 1, para, "Next Page" })); //Pictures on the next page (("<a href=\"?page={0}{1}\">{2}</a>", new object[] { page + 1, para, "<img src=\"images/\" border=\"0\"/>" })); } break; } //Picture on the last page (("&nbsp;&nbsp;<a href=\"?page={0}{1}\"><img src=\"images/\" border=\"0\"/></a>&nbsp;&nbsp;", new object[] { sumpage, para })); } (("The{0}Page/common{1}Page common{2}strip", new object[] { page, sumpage, count })); } return (); } public static string paging3(string url, string para, int sumpage, int page, int count) { string result = ; if (page > sumpage) { page = 1; } StringBuilder sb = new StringBuilder(); if (sumpage > 0) { if (sumpage != 1) { //Page 1 (("<a href=\"{2}?page={0}{1}\">Home</a>", new object[] { 1, para, url })); switch (page) { case 1: //Picture on the previous page // (("<a>{0}</a>", new object[] { "<img src=\"images/\" border=\"0\"/>" })); (("<a>superior一Page</a><a href=\"{3}?page={0}{1}\">{2}</a> ", new object[] { page + 1, para, "Next Page", url })); //Pictures on the next page // (("<a href=\"?page={0}{1}\">{2}</a>", new object[] { page + 1, para, "<img src=\"images/\" border=\"0\"/>" })); break; default: if (sumpage == page) { //Picture on the previous page //(("<a href=\"?page={0}{1}\">{2}</a>", new object[] { page - 1, para, "<img src=\"images/\" border=\"0\"/>" })); (("<a href=\"{3}?page={0}{1}\">{2}</a><a>Down一Page</a> ", new object[] { page - 1, para, "Previous Page", url })); //Pictures on the next page //(("<a>{0}</a>", new object[] { "<img src=\"images/\" />" })); } else { //Picture on the previous page //(("<a href=\"?page={0}{1}\">{2}</a>", new object[] { page - 1, para, "<img src=\"images/\" border=\"0\"/>" })); (("<a href=\"{6}?page={0}{1}\">{2}</a> <a href=\"{6}?page={3}{4}\">{5}</a> ", new object[] { page - 1, para, "Previous Page", page + 1, para, "Next Page", url })); //Pictures on the next page //(("<a href=\"?page={0}{1}\">{2}</a>", new object[] { page + 1, para, "<img src=\"images/\" border=\"0\"/>" })); } break; } //Picture on the last page (("<a href=\"{2}?page={0}{1}\">Last Page</a> ", new object[] { sumpage, para, url })); } (("The{0}Page/common{1}Page common{2}strip", new object[] { page, sumpage, count })); } return (); } #endregion #region Date format judgment /// <summary> /// Date format string judgment /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool IsDateTime(string str) { try { if (!(str)) { (str); return true; } else { return false; } } catch { return false; } } #endregion Whether #region consists of specific characters public static bool isContainSameChar(string strInput) { string charInput = ; if (!(strInput)) { charInput = (0, 1); } return isContainSameChar(strInput, charInput, ); } public static bool isContainSameChar(string strInput, string charInput, int lenInput) { if ((charInput)) { return false; } else { Regex RegNumber = new Regex(("^([{0}])+$", charInput)); //Regex RegNumber = new Regex(("^([{0}]{{1}})+$", charInput,lenInput)); Match m = (strInput); return ; } } #endregion #region Check whether the input parameters are some defined special characters: This method is currently used for security checks for password input /// <summary> /// Check whether the input parameters are some defined special characters: This method is currently used for security checks for password input /// </summary> public static bool isContainSpecChar(string strInput) { string[] list = new string[] { "123456", "654321" }; bool result = new bool(); for (int i = 0; i < ; i++) { if (strInput == list[i]) { result = true; break; } } return result; } #endregion } }
I hope this article will be helpful to everyone's C# programming.