Asp.net background verification class

Source: Internet
Author: User

/// <Summary>
/// Provide some verification logic that is frequently used. For example, whether the email address is valid
/// </Summary>
Public class validator
{
/// <Summary>
/// Check whether a string can be converted to a date. It is generally used to verify the validity of the date entered by the user.
/// </Summary>
/// <Param name = "_ value"> string to be verified. </Param>
/// <Returns> whether it can be converted to the bool value of the date. </Returns>
Public static bool isstringdate (string _ value)
{
Datetime dtime;
Try
{
Dtime = datetime. parse (_ value );
}
Catch (formatexception E)
{
// When the date format is incorrect
Console. writeline (E. Message );
Return false;
}
Return true;
}
/// <Summary>
/// Check for Chinese Characters
/// </Summary>
/// <Param name = "inputdata"> </param>
/// <Returns> </returns>
Public static bool ishaschzn (string inputdata)
{
RegEx regchzn = new RegEx ("[/u4e00-/u9fa5]");
Match m = regchzn. Match (inputdata );
Return M. success;
}
/// <Summary>
/// Check whether a string is composed of pure numbers. It is generally used to query the validity verification of string parameters.
/// </Summary>
/// <Param name = "_ value"> the string to be verified .. </Param>
/// <Returns> whether the bool value is valid. </Returns>
Public static bool isnumeric (string _ value)
{
Return validator. quickvalidate ("^ [1-9] * [0-9] * $", _ value );
}

/// <Summary>
/// Check whether a string is composed of pure letters and numbers. It is generally used to query the validity verification of string parameters.
/// </Summary>
/// <Param name = "_ value"> string to be verified. </Param>
/// <Returns> whether the bool value is valid. </Returns>
Public static bool isletterornumber (string _ value)
{
Return validator. quickvalidate ("^ [a-zA-Z0-9 _] * $", _ value );
}

/// <Summary>
/// Determine whether it is a number, including decimals and integers.
/// </Summary>
/// <Param name = "_ value"> string to be verified. </Param>
/// <Returns> whether the bool value is valid. </Returns>
Public static bool isnumber (string _ value)
{
Return validator. quickvalidate ("^ (0 | ([1-9] + [0-9] *) (. [0-9] + )? $ ", _ Value );
}

/// <Summary>
/// Quickly verify whether a string matches the specified regular expression.
/// </Summary>
/// <Param name = "_ Express"> content of the regular expression. </Param>
/// <Param name = "_ value"> string to be verified. </Param>
/// <Returns> whether the bool value is valid. </Returns>
Public static bool quickvalidate (string _ Express, string _ value)
{
System. Text. regularexpressions. RegEx myregex = new system. Text. regularexpressions. RegEx (_ express );
If (_ value. Length = 0)
{
Return false;
}
Return myregex. ismatch (_ value );
}
/// <Summary>
/// Determine whether a string is an email
/// </Summary>
/// <Param name = "_ value"> </param>
/// <Returns> </returns>
Public static bool isemail (string _ value)
{
RegEx = new RegEx (@ "^/W + ([-+.] /W +) * @ (/W + ([-.] /W + )*/.) + ([A-Za-Z] +) + $ ", regexoptions. ignorecase );
Return RegEx. Match (_ value). success;
}
/// <Summary>
/// Determine whether a string is in the ID format
/// </Summary>
/// <Param name = "_ value"> </param>
/// <Returns> </returns>
Public static bool isidcard (string _ value)
{
RegEx;
String [] strarray;
Datetime time;
If (_ value. length! = 15) & (_ value. length! = 0x12 ))
{
Return false;
}
If (_ value. Length = 15)
{
RegEx = new RegEx (@ "^ (/d {6}) (/d {2}) (/d {2}) (/d {2 }) (/d {3}) $ ");
If (! RegEx. Match (_ value). Success)
{
Return false;
}
Strarray = RegEx. Split (_ value );
Try
{
Time = new datetime (Int. parse ("19" + strarray [2]), Int. parse (strarray [3]), Int. parse (strarray [4]);
Return true;
}
Catch
{
Return false;
}
}
RegEx = new RegEx (@ "^ (/d {6}) (/d {4}) (/d {2}) (/d {2 }) (/d {3}) ([0-9xx]) $ ");
If (! RegEx. Match (_ value). Success)
{
Return false;
}
Strarray = RegEx. Split (_ value );
Try
{
Time = new datetime (Int. parse (strarray [2]), Int. parse (strarray [3]), Int. parse (strarray [4]);
Return true;
}
Catch
{
Return false;
}
}
/// <Summary>
/// Determine whether a string is int
/// </Summary>
/// <Param name = "_ value"> </param>
/// <Returns> </returns>
Public static bool isint (string _ value)
{
RegEx = new RegEx (@ "^ (-) {0, 1}/d + $ ");
If (RegEx. Match (_ value). Success)
{
If (long. parse (_ value)> 0x7fffffl) | (long. parse (_ value) <-2147483648l ))
{
Return false;
}
Return true;
}
Return false;
}

/// <Summary>
/// Determine whether a string is a mobile phone number
/// </Summary>
/// <Param name = "_ value"> </param>
/// <Returns> </returns>
Public static bool ismobilenum (string _ value)
{
RegEx = new RegEx (@ "^ 13/d {9} $", regexoptions. ignorecase );
Return RegEx. Match (_ value). success;
}
/// <Summary>
/// Determine whether a string is a telephone number
/// </Summary>
/// <Param name = "_ value"> </param>
/// <Returns> </returns>
Public static bool isphonenum (string _ value)
{
RegEx = new RegEx (@ "^ (86 )? (-)? (0/d {2, 3 })? (-)? (/D {7, 8 })(-)? (/D {3, 5 })? $ ", Regexoptions. ignorecase );
Return RegEx. Match (_ value). success;
}
/// <Summary>
/// Determine whether a string is a URL
/// </Summary>
/// <Param name = "_ value"> </param>
/// <Returns> </returns>
Public static bool isurl (string _ value)
{
RegEx = new RegEx (@ "(http ://)? ([/W-] +/.) * [/W-] + (/[/W -./? % & =] *)? ", Regexoptions. ignorecase );
Return RegEx. Match (_ value). success;
}
/// <Summary>
/// Determine whether a string contains letters and numbers
/// RegEx ("[a-zA-Z0-9]? "
/// </Summary>
/// <Param name = "_ value"> </param>
/// <Returns> </returns>
Public static bool iswordandnum (string _ value)
{
RegEx = new RegEx ("[a-zA-Z0-9]? ");
Return RegEx. Match (_ value). success;
}
/// <Summary>
/// Convert string to date
/// </Summary>
/// <Param name = "_ value"> string </param>
/// <Param name = "_ defaultvalue"> default value </param>
/// <Returns> </returns>
Public static datetime strtodate (string _ value, datetime _ defaultvalue)
{
If (isstringdate (_ value ))
Return convert. todatetime (_ value );
Else
Return _ defaultvalue;
}
/// <Summary>
/// Convert the string into an integer
/// </Summary>
/// <Param name = "_ value"> string </param>
/// <Param name = "_ defaultvalue"> default value </param>
/// <Returns> </returns>
Public static int strtoint (string _ value, int _ defaultvalue)
{
If (isnumber (_ value ))
Return Int. parse (_ value );
Else
Return _ defaultvalue;
}
}

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.