ASP. NET regular expression to verify various character classes
Namespace Toolkit
{
Using system;
Using system. Text. regularexpressions;
Public class validatorhelper
{
/// <Summary>
/// Email
/// </Summary>
/// <Param name = "_ value"> </param>
/// <Returns> </returns>
Public static bool isemail (string _ value)
{
// @ "^ \ W + ([-+.] \ W +) * @ (\ W + ([-.] \ W + )*\.) + ([A-Za-Z] +) + _
RegEx = new RegEx (@ "\ W + ([-+. '] \ W +) * @ \ W + ([-.] \ W + )*\. \ W + ([-.] \ W +) * ", regexoptions. ignorecase );
Return RegEx. Match (_ value). success;
}
/// <Summary>
/// Verify whether the Chinese ID is 15 or 18 characters
/// </Summary>
/// <Param name = "ID"> </param>
/// <Returns> </returns>
Public static bool isvalidcnid (string ID)
{// Verify whether the ID card is 15 or 18 characters long
RegEx = new RegEx (@ "d {18} | D {15 }");
Return RegEx. ismatch (ID );
}
/// <Summary>
/// ID card
/// </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>
/// Whether it 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;
}
Public static bool islengthstr (string _ value, int _ begin, int _ end)
{
Int length = _ value. length;
If (length <_ begin) & (length> _ end ))
{
Return false;
}
Return true;
}
/// <Summary>
/// Only characters
/// </Summary>
/// <Param name = "character"> </param>
/// <Returns> </returns>
Public static bool isonlycharacters (string character)
{
RegEx = new RegEx (@ "^. [A-Za-Z] + $ ");
Return RegEx. ismatch (character );
}
Public static bool isletterornumber (string _ value)
{
Return quickvalidate ("^ [a-zA-Z0-9 _] * _", _ value );
}
/// <Summary>
/// Verify the Chinese mobile phone number
/// </Summary>
/// <Param name = "_ value"> </param>
/// <Returns> </returns>
Public static bool iscnmobilenum (string _ value)
{
// 13 \ D {9 }_
RegEx = new RegEx (@ "^ (\ D {3} \) | (\ D {3 }\-))? 13 [0-9] \ D {8} | 15 [0-9] \ D {8} | 18 [0-9] \ D {8} ", regexoptions. ignorecase );
Return RegEx. Match (_ value). success;
}
/// <Summary>
/// Verify the Chinese phone number
/// </Summary>
/// <Param name = "phone"> </param>
/// <Returns> </returns>
Public static bool iscnphonenum (string _ value)
{
// ^ (86 )? (-)? (0 \ D {2, 3 })? (-)? (\ D {7, 8 })(-)? (\ D {3, 5 })? _
RegEx = new RegEx (@ "(^ [0-9] {3, 4} \-[0-9] {3, 8} $) | (^ [0-9] {3, 8} $) | (^ \ ([0-9] {3, 4} \) [0-9] {3, 8} $) | (^ 0 {0, 1} 13 [0-9] {9} ", regexoptions. ignorecase );
Return RegEx. Match (_ value). success;
}
/// <Summary>
/// Verify the China Region Code
/// </Summary>
/// <Param name = "phone"> </param>
/// <Returns> </returns>
Public static bool isvalidcnzipcode (string zipcode)
{
RegEx = new RegEx (@ "d {6 }");
Return RegEx. ismatch (zipcode );
}
/// <Summary>
/// Verify the US phone number
/// </Summary>
/// <Param name = "phone"> </param>
/// <Returns> </returns>
Public static bool isvalidusphone (string phone)
{
RegEx = new RegEx (@ "(\ D {3 }\)?) | (\ D {3 }-))? \ D {3}-\ D {4 }");
Return RegEx. ismatch (phone );
}
/// <Summary>
/// Verify the U.S. zip code
/// </Summary>
/// <Param name = "phone"> </param>
/// <Returns> </returns>
Public static bool isvaliduszipcode (string zipcode)
{
RegEx = new RegEx (@ "\ D {5} (-\ D {4 })? ");
Return RegEx. ismatch (zipcode );
}
/// <Summary>
/// Only a number
/// </Summary>
/// <Param name = "_ value"> </param>
/// <Returns> </returns>
Public static bool isnumber (string _ value)
{
// ^ (0 | ([1-9] + [0-9] *) (. [0-9] + )? _
Return quickvalidate ("^. [0-9] * $", _ value );
}
Public static bool isnumeric (string _ value)
{
Return quickvalidate ("^ [1-9] * [0-9] * _", _ value );
}
/// <Summary>
/// The verification date type is yyyy-mm-dd.
/// </Summary>
/// <Param name = "date"> </param>
/// <Returns> </returns>
Public static bool isvaliddate (string date)
{// Verification date type: yyyy-mm-dd
RegEx = new RegEx (@ "^ (19 | 20) ([02468] [048]) | ([13579] [26]) -02-29) | (20 [0-9] [0-9]) | (19 [0-9] [0-9]) -(0 [1-9]) | (1 [0-2])-(0 [1-9]) | (1 \ D) | (2 [0-8]) | (0 [13578]) | (1 [02])-31) | (0 [1-9]) | (1 [0-2])-(29 | 30) $ ");
Return RegEx. ismatch (date );
}
/// <Summary>
/// Verification date type: yyyy-mm-dd hh: mm: SS
/// </Summary>
/// <Param name = "date"> </param>
/// <Returns> </returns>
Public static bool isvaliddatetime (string datetime)
{// Verification date type: yyyy-mm-dd hh: mm: SS
RegEx = new RegEx (@ "^ (1 [6-9] | [2-9] \ D) \ D {2})-(0? [1, 13578] | 1 [02])-(0? [1-9] | [12] \ d | 3 [01]) | (1 [6-9] | [2-9] \ D) \ D {2})-(0? [13456789] | 1 [012])-(0? [1-9] | [12] \ d | 30) | (1 [6-9] | [2-9] \ D) \ D {2 }) -0? 2-(0? [1-9] | 1 \ d | 2 [0-8]) | (1 [6-9] | [2-9] \ D) (0 [48] | [2468] [048] | [13579] [26]) | (16 | [2468] [048] | [3579] [26]) 00)-0? 2-29-) (20 | 21 | 22 | 23 | [0-1]? \ D): [0-5]? \ D: [0-5]? \ D $ ");
Return RegEx. ismatch (datetime );
}
Public static bool isstringdate (string _ value)
{
Try
{
Datetime dtime = datetime. parse (_ value );
}
Catch (formatexception E)
{
Console. writeline (E. Message );
Return false;
}
Return true;
}
/// <Summary>
/// URL
/// </Summary>
/// <Param name = "_ value"> </param>
/// <Returns> </returns>
Public static bool isurl (string _ value)
{
// (Http ://)? ([\ W-] + \.) * [\ W-] + (/[\ W -./? % & =] *)?
RegEx = new RegEx (@ "HTTP (s )? : // ([\ W-] + \.) + [\ W-] + (/[\ W -./? % & =] *)? ", Regexoptions. ignorecase );
Return RegEx. Match (_ value). success;
}
/// <Summary>
/// Whether it is a letter or number
/// </Summary>
/// <Param name = "_ value"> </param>
/// <Param name = "_ defaultvalue"> </param>
/// <Returns> </returns>
Public static bool iswordandnum (string _ value)
{
RegEx = new RegEx ("[a-zA-Z0-9]? ");
Return RegEx. Match (_ value). success;
}
/// <Summary>
/// Quick verification
/// </Summary>
/// <Param name = "_ value"> </param>
/// <Param name = "_ defaultvalue"> </param>
/// <Returns> </returns>
Public static bool quickvalidate (string _ Express, string _ value)
{
RegEx myregex = new RegEx (_ express );
If (_ value. Length = 0)
{
Return false;
}
Return myregex. ismatch (_ value );
}
/// <Summary>
/// String to date
/// </Summary>
/// <Param name = "_ value"> </param>
/// <Param name = "_ defaultvalue"> </param>
/// <Returns> </returns>
Public static datetime strtodate (string _ value, datetime _ defaultvalue)
{
If (isstringdate (_ value ))
{
Return convert. todatetime (_ value );
}
Return _ defaultvalue;
}
/// <Summary>
/// Convert string to int
/// </Summary>
/// <Param name = "_ value"> </param>
/// <Param name = "_ defaultvalue"> </param>
/// <Returns> </returns>
Public static int strtoint (string _ value, int _ defaultvalue)
{
If (isnumber (_ value ))
{
Return Int. parse (_ value );
}
Return _ defaultvalue;
}
/// <Summary>
/// Verify whether it is Korean
/// </Summary>
/// <Param name = "Korean"> </param>
/// <Returns> </returns>
Public static bool isvalidkorean (string Korean)
{
RegEx = new RegEx (@ "^. [\ uac00-\ ud7af \ u1100-\ u11ff \ u3130-\ u318f] + $ ");
Return RegEx. ismatch (Korean );
}
}
}
What is a regular expression?
Http://baike.baidu.com/view/94238.htm
Http://baike.baidu.com/view/1091115.htm
Online Test Regular Expression
Http://regexpal.com/
Http://www.nregex.com/
Http://regex.larsolavtorvik.com/
Http://www.myregexp.com/
Http://www.rubular.com/
Http://osteele.com/tools/reanimator