asp.net type conversion class (Universal Class) code sharing _ Practical Tips

Source: Internet
Author: User
Tags encode string static class

Nonsense not much said, directly to everyone to paste the code, the specific code as follows:

 <summary>///type conversion class///handle the case where the database fetch field is empty///</summary> public static class Dbconvert {#region---- --------------ToInt32 type conversion------------------///<summary>///Read the string in the database and convert it to Int32///Null to return 0///</summary >///<param name= "obj" >object type of value </param>///<returns>int32 type </returns> public static I
   NT ToInt32 (object obj) {int result = 0;
   if (Isint (convert.tostring (obj)) {result = Convert.ToInt32 (obj); } else if (obj!= null && obj is enum)//handling non-null value type (or enum) {result = ((iconvertible) obj).
   ToInt32 (NULL);
  return result; ///<summary>///reads the string in the database and converts it to Int32///null returns 0///</summary>///<param name= "str" >string class  Type value </param>///<returns>int32 type </returns> public static int ToInt32 (string str) {int =
   0;
   if (Isint (str)) {result = Convert.ToInt32 (str);
  return result; }///<summary>///Determine if a string is of type int///If yes returns true if it is not returned false///</summary>///<param name= "str" >string value </param&
  Gt
  <returns>true: is a string of int (that is, it can be converted to an int type), false: Not a string of type int </returns> public static bool Isint (String str)
   {bool result = false;
    if (str!= "" && str!=null) {regex reg = new Regex ("^[0-9]*$"); if (Reg.
    IsMatch (str)) {result = true;
  } return result;
  #endregion #region------------------tostring type conversion------------------///<summary>///Read the strings in the database and convert to string </summary>///<param name= "obj" >object type of value </param>///<returns>string type </returns&gt
  ;
   public static string ToString (Object obj) {string result = ' ";
   if (obj!= null) {result = Convert.ToString (obj);
  return result; #endregion #region------------------todouble type conversions------------------///<summary>///Determine whether a string is of type double (package Enclose negative floating-point type)///If yes returns true, if not return false
  </summary>///<param name= "str" >string type of value </param>///<returns>true: is a double string (that is, you can convert
   A double type), false: Not a double string </returns> public static bool Isdouble (string str) {bool = false;
    if (str!= "" && str!= null) {regex reg = new Regex (@ ^ (-?\d+) (\.\d+)? $); if (Reg.
    IsMatch (str)) {result = true;
  } return result; ///<summary>///reads the string in the database and converts it to Int32///null returns 0///</summary>///<param name= "obj" >object class Type value </param>///<returns>int32 type </returns> public static double ToDouble (object obj) {Double R
   Esult = 0.0;
   if (isdouble (convert.tostring (obj)) {result = Convert.todouble (obj); ' Else if ' (obj!= null && obj is enum)//process enum {result = ((iconvertible) obj).
   ToDouble (NULL);
  return result; ///<summary>///reads the string in the database and converts it to Int32///null returns 0///</summary>///<param Name= "str" >string type value </param>///<returns>int32 type </returns> public static double ToDouble (String St
   R) {double result = 0.0;
   if (isdouble (str)) {result = Convert.todouble (str);
  return result;  #endregion #region------------------todatetime type conversions------------------///<summary>///Determine whether the time format is a time type/// such as 23:10///</summary>///<param name= "str" > the string to be judged </param>///<returns>true: is a string of time type (that is, you can
   Convert to Time type), false: Not a string of time type </returns> public static bool Isdatetime (string str) {bool = false; if (str!= "" && str!= null) {regex reg = new Regex (([01]\\d) | (
    2[0-3]): [0-5]\\d]); if (Reg.
    IsMatch (str)) {result = true;
  } return result; } #endregion}//"^\d+ (\.\d+) $"//Non-negative floating-point number (positive floating-point number + 0)//"^ ([0-9]+\.[ 0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\. [0-9]+) | ([0-9]*[1-9][0-9]*)] $ "//positive floating-point number///" ^ (-\d+ (\.\d+)?) | (0+ (\.0+)) $ "//non-positive floating-point number (negative floating-point number + 0)//" ^ (-) ([0-9]+\. [0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\. [0-9]+) | ([0-9]*[1-9][0-9]*))] $ "//negative floating-point number//" ^ (-?\d+) (\.\d+)? $ "//floating-point number

OK, asp.net type conversion class (generic Class) code sharing ends here.

Below, look at the example code for the asp.net page data validation generic class.

public class Pagevalidate {private static regex regphone = new Regex ("^[0-9]+[-]?[ 0-9]+[-]?
[0-9]$ ");
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 alphanumeric string, as in [a-za-z0-9] syntax private static Regex REGCHZN = new Regex (" [\
U4E00-\U9FA5] "); Public pagevalidate () {}//numeric string check #region numeric string check public static bool Isphone (string inputdata) {Match m = Regphone.match
(Inputdata);
return m.success; /**////<summary>///checks the key value of the Request query string, whether it is a number, the maximum length limit///</summary>///<param name= "req" >request </param>///<param name= "Inputkey" >request key value </param>///<param name= "MaxLen" > Max length </ Param>///<returns> return REquest query string </returns> public static string Fetchinputdigit (HttpRequest req, string inputkey, int maxlen) {string re Tval = 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; /**////<summary>///Whether numeric string///</summary>///<param name= "inputdata" > Input string </param>///< returns></returns> public static bool Isnumber (string inputdata) {Match m = Regnumber.match (Inputdata); .
Success; /**////<summary>///Whether numeric strings can be signed///</summary>///<param name= "inputdata" > Input string </param>// /<returns></returns> public static bool Isnumbersign (string inputdata) {Match m = regnumbersign.match (input
Data);
return m.success; }/**////<summary>///whether it isFloating point number///</summary>///<param name= "inputdata" > Input string </param>///<returns></returns> public static bool Isdecimal (string inputdata) {Match m = Regdecimal.match (Inputdata); return m.success;}/**////<su Mmary>///is a floating-point number with positive and negative///</summary>///<param name= "inputdata" > Input string </param>///<returns ></returns> public static bool Isdecimalsign (string inputdata) {Match m = Regdecimalsign.match (inputdata); Retu
RN m.success; #endregion//Chinese detection #region/**////<summary>///detect if there are Chinese characters///</summary>///<param name= "Inputdata" & gt;</param>///<returns></returns> public static bool Ishaschzn (string inputdata) {Match m = regchzn.m
Atch (Inputdata);
return m.success; #endregion//mail address #region mail address/**////<summary>///is a floating-point number with positive and negative///</summary>///<param name= "Inputdat A "> Input string </param>///<returns></returns> public static bool Isemail (string inputdata) {MAtch m = Regemail.match (Inputdata);
return m.success; #endregion//Other #region other/**////<summary>///Check the maximum length of the string, return a specified length of string///</summary>///<param name= "Sqlinp
UT "> Input string </param>///<param name=" maxLength "> Max length </param>///<returns></returns> public static string SQLText (string sqlinput, int maxLength) {if (sqlinput!= null && sqlinput!= string. Empty) {sqlinput = Sqlinput.trim (); if (Sqlinput.length > MaxLength)//To intercept the string by the maximum length sqlinput = sqlinput.substring (0, MaxL
Ength);
return sqlinput; /**////<summary>///string encoding///</summary>///<param name= "Inputdata" ></param>///<returns ></returns> public static string HtmlEncode (String inputdata) {return httputility.htmlencode (inputdata);}/** <summary>///Settings Label displays encode string///</summary>///<param name= "LBL" ></param>///< param name= "txtinput" ></param> public static void SetLabel (Label lbl, String txtInput) {lbl.
Text = HtmlEncode (txtinput); public static void SetLabel (Label lbl, Object inputobj) {SetLabel (LBL, inputobj.tostring ());}//String cleanup public static St  Ring Inputtext (string inputstring, int maxLength) {StringBuilder retVal = new StringBuilder ();//Check for empty if (inputstring  != null) && (inputstring!= String.Empty)) {inputstring = Inputstring.trim ();//Check length if (Inputstring.length >
maxLength) inputstring = inputstring.substring (0, maxLength);
Replace the dangerous character for (int i = 0; i < inputstring.length i++) {switch (inputstring[i)) {case ' "': Retval.append (" ");
Break
Case ' < ': Retval.append ("<");
Break
Case ' > ': Retval.append (">");
Break
Default:retVal.Append (Inputstring[i]);
Break
} retval.replace ("'", "");//replace single quotes} return retval.tostring ();} /**////<summary>///converted to HTML code///</summary>///<param name= "str" >string</param>///<re turns>string</returns> public static string Encode (String str) {str = str.Replace ("&", "&"); str = str.
Replace ("'", "" "); str = str.
Replace ("\" "," ""); str = str.
Replace ("", " "); str = str.
Replace ("<", "<"); str = str.
Replace (">", ">"); str = str.
Replace ("\ n", "<br>");
return str; /**////<summary>///Parsing html to normal text///</summary>///<param name= "str" >string</param>///<r eturns>string</returns> public static string Decode (String str) {str = str.
Replace ("<br>", "\ n"); str = str.
Replace (">", ">"); str = str.
Replace ("<", "<"); str = str.
Replace (" ", ""); str = str.
Replace ("" "," \ ");
return str; public static string Sqltextclear (String sqltext) {if (SQLText = null) {return null;} if (SQLText = = "") {return ""
; } SQLText = Sqltext.replace (",", "");/removal, sqltext = Sqltext.replace ("<", "");/removal < SQLText = Sqltext.replace ("> "," ");//remove > SQLText = Sqltext.replace ("--"," ");//Remove-SQLText = Sqltext.replace (" "," ");//remove ' SQLText = Sqltext.repl Ace ("\" "," "");//Remove "sq"Ltext = sqltext.replace ("=", "");/remove = SQLText = Sqltext.replace ("%", "");//Remove% SQLText = Sqltext.replace ("", "");//Remove spaces
return sqltext; #endregion//Whether it consists of a specific character #region whether a particular character constitutes public static bool Iscontainsamechar (string strinput) {string charinput = Strin
G.empty; if (!string. IsNullOrEmpty (strinput)) {charinput = strinput.substring (0, 1);} return Iscontainsamechar (Strinput, Charinput,
Strinput.length); public static bool Iscontainsamechar (string strinput, string charinput, int leninput) {if (string). IsNullOrEmpty (Charinput)) {return false;} else {regex regnumber = new Regex (string.
Format ("^ ([{0}]) +$", charinput); Regex regnumber = new Regex (string.
Format ("^ ([{0}]{{1}}}) +$", charinput,leninput));
Match m = Regnumber.match (strinput);
return m.success; } #endregion//Check that the input parameters are not some well-defined special characters: This method is currently used for password entry security checks #region Check that the input parameter is not some defined special character: This method is currently used for password entry security/**////< Summary>///Check that the input parameters are not some well-defined special characters: This method is currently used for password entry security checks///</summary> public static bool Iscontainspecchar (String strinput) {string[] list = new string[] {"123456", "654321"}; bool result = new bool (); for (int i = 0; i < l Ist. Length;
i++) {if (strinput = = List[i]) {result = true; } #endregion}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.