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) $ "); // a string of letters or numbers, the same as the [a-zA-Z0-9] syntax
Private Static RegEx regchzn = new RegEx ("[/u4e00-/u9fa5]");
Public pagevalidate ()
{
}
# Region numeric string check
/// <Summary>
/// Check whether the key value of the request string is a number and the maximum length is limited.
/// </Summary>
/// <Param name = "req"> request </param>
/// <Param name = "inputkey"> request 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 = 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 it is a numeric string
/// </Summary>
/// <Param name = "inputdata"> input string </param>
/// <Returns> </returns>
Public static bool isnumber (string inputdata)
{
Match m = regnumber. Match (inputdata );
Return M. success;
}
/// <Summary>
/// Whether a numeric string can contain positive and negative numbers
/// </Summary>
/// <Param name = "inputdata"> input string </param>
/// <Returns> </returns>
Public static bool isnumbersign (string inputdata)
{
Match m = regnumbersign. Match (inputdata );
Return M. success;
}
/// <Summary>
/// Whether it is a floating point
/// </Summary>
/// <Param name = "inputdata"> input string </param>
/// <Returns> </returns>
Public static bool isdecimal (string inputdata)
{
Match m = regdecimal. Match (inputdata );
Return M. success;
}
/// <Summary>
/// Whether the floating point can contain positive and negative numbers
/// </Summary>
/// <Param name = "inputdata"> input string </param>
/// <Returns> </returns>
Public static bool isdecimalsign (string inputdata)
{
Match m = regdecimalsign. Match (inputdata );
Return M. success;
}
# Endregion
# Region Chinese Detection
/// <Summary>
/// Check for Chinese Characters
/// </Summary>
/// <Param name = "inputdata"> </param>
/// <Returns> </returns>
Public static bool ishaschzn (string inputdata)
{
Match m = regchzn. Match (inputdata );
Return M. success;
}
# Endregion
# Region email address
/// <Summary>
/// Whether the floating point can contain positive and negative numbers
/// </Summary>
/// <Param name = "inputdata"> input string </param>
/// <Returns> </returns>
Public static bool isemail (string inputdata)
{
Match m = regemail. Match (inputdata );
Return M. success;
}
# Endregion
# Region others
/// <Summary>
/// Check the maximum length of a 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! = String. Empty)
{
Sqlinput = sqlinput. Trim ();
If (sqlinput. length> maxlength) // truncate a string by maximum length
Sqlinput = sqlinput. substring (0, maxlength );
}
Return sqlinput;
}
/// <Summary>
/// String Encoding
/// </Summary>
/// <Param name = "inputdata"> </param>
/// <Returns> </returns>
Public static string htmlencode (string inputdata)
{
Return httputility. htmlencode (inputdata );
}
/// <Summary>
/// Set the label to display the 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 string inputtext (string inputstring, int maxlength)
{
Stringbuilder retval = new stringbuilder ();
// Check whether it is empty
If (inputstring! = NULL) & (inputstring! = String. Empty ))
{
Inputstring = inputstring. Trim ();
// Check the length
If (inputstring. length> maxlength)
Inputstring = inputstring. substring (0, maxlength );
// Replace dangerous characters
For (INT I = 0; I <inputstring. length; I ++)
{
Switch (inputstring)
{
Case '"':
Retval. append (""");
Break;
Case '<':
Retval. append ("<");
Break;
Case '> ':
Retval. append ("> ");
Break;
Default:
Retval. append (inputstring);
Break;
}
}
Retval. Replace ("'", ""); // replace single quotes
}
Return retval. tostring ();
}
/// <Summary>
/// Convert to HTML code
/// </Summary>
/// <Param name = "str"> string </param>
/// <Returns> 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>
/// Parse HTML into plain text
/// </Summary>
/// <Param name = "str"> string </param>
/// <Returns> 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;
}
# Endregion
}