The following lists various forms of verification functions that are commonly used in development, including email, phone, ip, website, date, ID card, etc.
/** // <Summary>
/// Verify the email address
/// </Summary>
/// <Param name = "source"> </param>
/// <Returns> </returns>
Public static bool IsEmail (string source)
{
Return Regex. IsMatch (source, @ "^ [A-Za-z0-9] ([_.-]? [A-zA-Z0-9] +) *) @ ([A-Za-z0-9] +) ([.-]? [A-zA-Z0-9] +) *). ([A-Za-z] {2,}) $ ", RegexOptions. IgnoreCase );
}
Public static bool HasEmail (string source)
{
Return Regex. IsMatch (source, @ "[A-Za-z0-9] ([_.-]? [A-zA-Z0-9] +) *) @ ([A-Za-z0-9] +) ([.-]? [A-zA-Z0-9] +) *). ([A-Za-z] {2,}) ", RegexOptions. IgnoreCase );
}
# Endregion
# Region verification URL
/** // <Summary>
/// Verify the URL
/// </Summary>
/// <Param name = "source"> </param>
/// <Returns> </returns>
Public static bool IsUrl (string source)
{
Return Regex. isMatch (source, @ "^ (file | gopher | news | nntp | telnet | http | ftp | https tutorial | ftps | sftp )://) | (www .)) + ([a-zA-Z0-9. _-] +. [a-zA-Z] {2, 6}) | ([0-9] {1, 3 }. [0-9] {1, 3 }. [0-9] {1, 3 }. [0-9] {}) (/[a-zA-Z0-9 & amp; % _. /-~ -] *)? $ ", RegexOptions. IgnoreCase );
}
Public static bool HasUrl (string source)
{
Return Regex. isMatch (source, @ "(file | gopher | news | nntp | telnet | http | ftp | https | ftps | sftp): //) | (www .)) + ([a-zA-Z0-9. _-] +. [a-zA-Z] {2, 6}) | ([0-9] {1, 3 }. [0-9] {1, 3 }. [0-9] {1, 3 }. [0-9] {}) (/[a-zA-Z0-9 & amp; % _. /-~ -] *)? ", RegexOptions. IgnoreCase );
}
# Endregion
# Region verification date
/** // <Summary>
/// Verification date
/// </Summary>
/// <Param name = "source"> </param>
/// <Returns> </returns>
Public static bool IsDateTime (string source)
{
Try
{
DateTime time = Convert. ToDateTime (source );
Return true;
}
Catch
{
Return false;
}
}
# Endregion
# Region verification mobile phone number
/** // <Summary>
/// Verify the mobile phone number
/// </Summary>
/// <Param name = "source"> </param>
/// <Returns> </returns>
Public static bool IsMobile (string source)
{
Return Regex. IsMatch (source, @ "^ 1 [35] d {9} $", RegexOptions. IgnoreCase );
}
Public static bool HasMobile (string source)
{
Return Regex. IsMatch (source, @ "1 [35] d {9}", RegexOptions. IgnoreCase );
}
# Endregion
# Region IP Address Verification
/** // <Summary>
/// Verify the IP address
/// </Summary>
/// <Param name = "source"> </param>
/// <Returns> </returns>
Public static bool IsIP (string source)
{
Return Regex. isMatch (source, @ "^ (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1-9] {1} [0-9] {1} | [1-9]). (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [1-9] | 0 ). (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [1-9] | 0 ). (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [0-9]) $ ", RegexOptions. ignoreCase );
}
Public static bool HasIP (string source)
{
Return Regex. isMatch (source, @ "(25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1-9] {1} [0-9] {1} | [1-9]). (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [1-9] | 0 ). (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [1-9] | 0 ). (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [0-9]) ", RegexOptions. ignoreCase );
}
# Endregion
# Region verify ID card validity
/** // <Summary>
/// Verify whether the ID card is valid
/// </Summary>
/// <Param name = "Id"> </param>
/// <Returns> </returns>
Public static bool IsIDCard (string Id)
{
If (Id. Length = 18)
{
Bool check = IsIDCard18 (Id );
Return check;
}
Else if (Id. Length = 15)
{
Bool check = IsIDCard15 (Id );
Return check;
}
Else
{
Return false;
}
}
Public static bool IsIDCard18 (string Id)
{
Long n = 0;
If (long. tryParse (Id. remove (17), out n) = false | n <Math. pow (10, 16) | long. tryParse (Id. replace ('X', '0 '). replace ('X', '0'), out n) = false)
{
Return false; // digit verification
}
String address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91 ";
If (address. IndexOf (Id. Remove (2) =-1)
{
Return false; // province verification
}
String birth = Id. Substring (6, 8). Insert (6, "-"). Insert (4 ,"-");
DateTime time = new DateTime ();
If (DateTime. TryParse (birth, out time) = false)
{
Return false; // birthday verification
}
String [] arrVarifyCode = (", 0, x, 9, 8, 4,"). Split (',');
String [] Wi = (","). Split (',');
Char [] Ai = Id. Remove (17). ToCharArray ();
Int sum = 0;
For (int I = 0; I <17; I ++)
{
Sum + = int. Parse (Wi [I]) * int. Parse (Ai [I]. ToString ());
}
Int y =-1;
Math. DivRem (sum, 11, out y );
If (arrVarifyCode [y]! = Id. Substring (17, 1). ToLower ())
{
Return false; // verification code
}
Return true; // conforms to GB11643-1999 standards
}
Public static bool IsIDCard15 (string Id)
{
Long n = 0;
If (long. TryParse (Id, out n) = false | n <Math. Pow (10, 14 ))
{
Return false; // digit verification
}
String address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91 ";
If (address. IndexOf (Id. Remove (2) =-1)
{
Return false; // province verification
}
String birth = Id. Substring (6, 6). Insert (4, "-"). Insert (2 ,"-");
DateTime time = new DateTime ();
If (DateTime. TryParse (birth, out time) = false)
{
Return false; // birthday verification
}
Return true; // meets the 15-digit ID card standard
}
# Endregion
# Is region Int type?
/** // <Summary>
/// Whether it is Int type
/// </Summary>
/// <Param name = "source"> </param>
/// <Returns> </returns>
Public static bool IsInt (string source)
{
Regex regex = new Regex (@ "^ (-) {0, 1} d + $ ");
If (regex. Match (source). Success)
{
If (long. Parse (source)> 0x7fffffl) | (long. Parse (source) <-2147483648L ))
{
Return false;
}
Return true;
}
Return false;
}
# Endregion
# Region: Check whether the length of a string is two characters in Chinese within a limit.
/** // <Summary>
/// Check whether the length of a string is two characters in Chinese between a limited number
/// </Summary>
/// <Param name = "source"> string </param>
/// <Param name = "begin"> greater than or equal to </param>
/// <Param name = "end"> less than or equal to </param>
/// <Returns> </returns>
Public static bool IsLengthStr (string source, int begin, int end)
{
Int length = Regex. Replace (source, @ "[^x00-xff]", "OK"). Length;
If (length <= begin) & (length> = end ))
{
Return false;
}
Return true;
}
# Endregion
# Is region a Chinese phone number in the format of 010-85849685?
/** // <Summary>
/// Is it a Chinese phone number, in the format of 010-85849685
/// </Summary>
/// <Param name = "source"> </param>
/// <Returns> </returns>
Public static bool IsTel (string source)
{
Return Regex. IsMatch (source, @ "^ d {3, 4 }-? D {6, 8} $ ", RegexOptions. IgnoreCase );
}
# Endregion
# Region ZIP Code 6 digits
/** // <Summary>
/// ZIP Code 6 digits
/// </Summary>
/// <Param name = "source"> </param>
/// <Returns> </returns>
Public static bool IsPostCode (string source)
{
Return Regex. IsMatch (source, @ "^ d {6} $", RegexOptions. IgnoreCase );
}
# Endregion
# Region Chinese
/** // <Summary>
/// Chinese
/// </Summary>
/// <Param name = "source"> </param>
/// <Returns> </returns>
Public static bool IsChinese (string source)
{
Return Regex. IsMatch (source, @ "^ [u4e00-u9fa5] + $", RegexOptions. IgnoreCase );
}
Public static bool hasChinese (string source)
{
Return Regex. IsMatch (source, @ "[u4e00-u9fa5] +", RegexOptions. IgnoreCase );
}
# Endregion
# Region check whether it is a normal combination of letters, numbers, and underscores
/** // <Summary>
/// Check whether a combination of letters, numbers, and underscores is normal.
/// </Summary>
/// <Param name = "source"> </param>
/// <Returns> </returns>
Public static bool IsNormalChar (string source)
{
Return Regex. IsMatch (source, @ "[wd _] +", RegexOptions. IgnoreCase );
}
# Endregion