C # verify whether the string is a number, whether it contains Chinese characters, whether it is a mailbox format, or whether it is a telephone format

Source: Internet
Author: User

Using system;
Using system. Web;
Using system. text;
Using system. Web. UI. webcontrols;
Using system. Text. regularexpressions;

Public class validatehelper
{
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] + $ ");
Private Static RegEx regemail = new RegEx ("^ [\ W-] + @ [\ W-] + \\. (COM | net | org | Edu | mil | TV | biz | info) $ ");
Private Static RegEx regchzn = new RegEx ("[\ u4e00-\ u9fa5]");
Private Static RegEx regtell = new RegEx ("^ ([0-9] {3.4}-) | [0-9 }-)? [0-9] {7, 8} $ ");
Private Static RegEx regsend = new RegEx ("[0-9] {1} ([0-9] +) {5 }");
Private Static RegEx regurl = new RegEx ("^ [A-Za-Z] +: // (\ W + (-\ W + )*)(\\. (\ W + (-\ W + )*))*(\\? \ S *)? | [A-Za-Z] + ://((? :(? : 25 [0-5] | 2 [0-4] \ d | [01]? \ D? \ D) \.) {3 }(? : 25 [0-5] | 2 [0-4] \ d | [01]? \ D? \ D) $ ");
Private Static RegEx regmobilephone = new RegEx ("^ 13 | 15 | 18 [0-9] {9} $ ");
Private Static RegEx regmoney = new RegEx ("^ [0-9] + | [0-9] + [.]? [0-9] + $ ");

# Region numeric string check

/// <Summary>
/// Whether it is a numeric string
/// </Summary>
/// <Param name = "inputdata"> input string </param>
Public static bool isnumber (string inputdata)
{
If (! String. isnullorempty (inputdata ))
{
Match m = regnumber. Match (inputdata );
Return M. success;
}
Else
{
Return false;
}
}

/// <Summary>
/// Whether a numeric string can contain positive and negative numbers
/// </Summary>
/// <Param name = "inputdata"> input string </param>
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>
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>
Public static bool isdecimalsign (string inputdata)
{
Match m = regdecimalsign. Match (inputdata );
Return M. success;
}

# Endregion

# Region Chinese Detection

/// <Summary>
/// Check for Chinese Characters
/// </Summary>
Public static bool ishaschzn (string inputdata)
{
Match m = regchzn. Match (inputdata );
Return M. success;
}

# Endregion

# Region email address
/// <Summary>
/// Whether the email address is used
/// </Summary>
/// <Param name = "inputdata"> input string </param>
Public static bool isemail (string inputdata)
{
Match m = regemail. Match (inputdata );
Return M. success;
}

# Endregion

# Region phone, zip code, network address, mobile phone number, price
/// <Summary>
/// Verify the phone number
/// </Summary>
Public static bool isphone (string inputdate)
{
If (! String. isnullorempty (inputdate ))
{
Match m = regtell. Match (inputdate );
Return M. success;
}
Else
{
Return false;
}
}
/// <Summary>
/// Whether it is a zip code
/// </Summary>
Public static bool issend (string inputdate)
{
If (! String. isnullorempty (inputdate ))
{
Match m = regsend. Match (inputdate );
Return M. success;
}
Else
{
Return false;
}
}
/// <Summary>
/// Whether it is a network address
/// </Summary>
Public static bool isurl (string inputdate)
{
Match m = regurl. Match (inputdate );
Return M. success;
}
/// <Summary>
/// Whether it is a mobile phone number
/// </Summary>
Public static bool ismobilephone (string inputdate)
{
Match m = regmobilephone. Match (inputdate );
Return M. success;
}
/// <Summary>
/// Whether it is a price
/// </Summary>
Public static bool ismoney (string inputdate)
{
Match m = regmoney. Match (inputdate );
Return M. success;
}
# Endregion
# Whether region is in the Time Format
/// <Summary>
/// Determine whether a string is in time format
/// </Summary>
/// <Param name = "inputdata"> input string </param>
/// <Returns> </returns>
Public static bool isdatetime (string inputdata)
{
Try
{
Convert. todatetime (inputdata );
Return true;
}
Catch
{
Return false;
}
}

# 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.