My data verification class pagevalidate [regular expression]

Source: Internet
Author: User
Tags encode string
  1. /*------------------------------------
    -- Company: Xi 'an Ancient Technology Development Co., Ltd.
    -- Project name: Xi'an Huantai Technology Business Office System
    -- Purpose: Data Verification Method
    -- Creation date:
    -- Created by Zhou haijun
    ------------------------------------*/
    Using system;
    Using system. Collections. Generic;
    Using system. text;
    Using system. Text. regularexpressions;
    Using system. Web;
    Using system. Web. UI. webcontrols;
    Namespace shanggu. Utility
    {
    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]");

# 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 common item Verification
/// <Summary>
/// ID card verification
/// </Summary>
/// <Param name = "personid"> </param>
/// <Returns> </returns>
Public static bool personid (string personid)
{
String idval = @ "^ (\ D {15}) (\ D {2} [Xx0-9])? $ ";
Return RegEx. ismatch (personid, idval );
}

/// <Summary>
/// Digit Verification
/// </Summary>
/// <Param name = "num"> </param>
/// <Returns> </returns>
Public static bool number (string num)
{
String numval = @ "^ [0-9] * [1-9] [0-9] * $ ";
Return RegEx. ismatch (Num, numval );
}

/// <Summary>
/// Phone Verification
/// </Summary>
/// <Param name = "tel"> </param>
/// <Returns> </returns>
Public static bool telphone (string Tel)
{
String telaval = @ "^ 0 [1-9] \ D {1, 2}-[1-9] \ D {6, 7} $ ";
String telval = @ "^ [1-9] \ D {6, 7} $ ";
String modval = @ "^ (159 | 156 | 153 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139) \ D {8} $ ";
Return RegEx. ismatch (Tel, telval) | RegEx. ismatch (Tel, modval) | RegEx. ismatch (Tel, telaval );
}
/// <Summary>
/// Verification Date Format (Format: yyyy-mm-dd)
/// </Summary>
/// <Param name = "datetime"> </param>
/// <Returns> </returns>
Public static bool datetimevalidate (string datetime)
{
String dateval =
@ "([0-9] {3} [1-9] | [0-9] {2} [1-9] [0-9] {1} | [0-9] {1} [1-9] [0-9] {2} | [1-9] [0-9] {3 }) -(0 [13578] | 1 [02])-(0 [1-9] | [12] [0-9] | 3 [01]) | (0 [469] | 11)-(0 [1-9] | [12] [0-9] | 30 )) | (02-(0 [1-9] | [1] [0-9] | 2 [0-8]) | ([0-9] {2}) (0 [48] | [2468] [048] | [13579] [26]) | (0 [48] | [2468] [048] | [3579] [26]) 00)-02-29 )";
Return RegEx. ismatch (datetime, dateval );
}
/// <Summary>
/// Verification Date Format (Format: yyyy/mm/DD)
/// </Summary>
/// <Param name = "datetime"> </param>
/// <Returns> </returns>
Public static bool datetime_validate (string datetime)
{
String dateval = "(0 [1-9] | [12] [0-9] | 3 [01]) /(0 [13578] | 1 [02]) | (0 [1-9] | [12] [0-9] | 30) /(0 [469] | 11) | (0 [1-9] | [1] [0-9] | 2 [0-8])/(02 )) /([0-9] {3} [1-9] | [0-9] {2} [1-9] [0-9] {1} | [0 -9] {1} [1-9] [0-9] {2} | [1-9] [0-9] {3 })) | (29/02/([0-9] {2}) (0 [48] | [2468] [048] | [13579] [26]) | (0 [48] | [2468] [048] | [3579] [26 )))";
Return RegEx. ismatch (datetime, dateval );
}
/// <Summary>
/// Email Verification
/// </Summary>
/// <Param name = "email"> </param>
/// <Returns> </returns>
Public static bool emailvalidate (string email)
{
String emailval = @ "^ ([\ W-\.] +) @ (\ [0-9] {1, 3 }\. [0-9] {1, 3 }\. [0-9] {1, 3 }\.) | ([\ W-] + \.) +) ([A-Za-Z] {2, 4} | [0-9] {1, 3}) (\]?) $ ";
Return RegEx. ismatch (email, emailval );

}

/// <Summary>
/// Postal code verification
/// </Summary>
/// <Param name = "postcode"> </param>
/// <Returns> </returns>
Public static bool postcodevalidate (string postcode)
{
String postval = @ "^ [1-9] \ D {5} $ ";
Return RegEx. ismatch (postcode, postval );
}

/// <Summary>
/// Amount Verification
/// </Summary>
/// <Param name = "moneycheck"> </param>
/// <Returns> </returns>
Public static bool money (string moneycheck)
{
String Monval = @ "^ ([1-9] \ D + | [0-9]) (\. \ D ?) * $ ";
Return RegEx. ismatch (moneycheck, Monval );
}
# Endregion

# Region Chinese Detection

    1. /// <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 ());
}
# Endregion
}
}

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.