C # general verification: email, phone, mobile phone, number, English, date, ID card, zip code, URL, IP address

Source: Internet
Author: User
Tags gopher nntp

The original copyright of this article belongs to Liu yongfa. for reprinting, please mark the original author to show respect!

Turn: http://www.cnblogs.com/KissFly/archive/2010/11/23/1885254.html

Namespace yongfa365.validator {using system; using system. text. regularexpressions;/*** // <summary> // Regexp soruce: http://regexlib.com/DisplayPatterns.aspx // Author: Liuyong method yongfa365 http://www.yongfa365.com/yongfa365@qq.com // intro: Verify the URL, IP, email, phone number, mobile phone number, number, English, date, ID card, zip code, // in principle, it is common in China. Because of the differences in various occasions, you must write it by yourself in special circumstances, here we can only provide some general verification, and it is unrealistic to pursue perfection. /// Version: 1.0 // puttime: // lastmodi: /// </Summary> // public class validator {# region verification email/*** /// <summary> // verify email /// </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 | 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 \ & % _\./-~ -] *)? $ ", 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 \ & % _\. /-~ -] *)? ", 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> // verification mobile phone number /// </Summary> /// <Param name = "Source"> </param> // <r Eturns> </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 verify IP/** // <summary> // verify IP /// </Summary> /// <Param name = "Source"> </param> // <returns> </returns> Public static bool is IP (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 whether the ID card is valid/*** // <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 '). R Eplace ('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) {RET Urn false; // birthday verification} string [] arrvarifycode = (", X "). split (','); string [] Wi = (", 2 "). 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 # Whether region is int type/*** // <summary> // whether it is int type // </Summary> /// <Param name = "Source"> </param> /// <returns> </returns> Public static bool isint (string source) {RegEx R Egex = new RegEx (@ "^ (-) {0, 1} \ D + $"); If (RegEx. match (source ). success) {If (Long. parse (source)> 0x7fffffffl) | (Long. parse (source) <-2147483648l) {return false;} return true;} return false ;} # endregion # region: Check whether the length of a string is a two-character Chinese character between the specified number. // <summary> // check whether the length of a string is between the specified number. one Chinese character is two characters /// </Summary> /// <Param name = "Source"> string </param> /// <Param name = "begin"> greater 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 # region is not a Chinese phone number, format: 010-85849685/*** // <summary> // is it a Chinese phone number, format: 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 verify whether it is a normal character combination of letters, numbers, and underscores/*** /// <summary> /// verify whether it is a normal character, letter, number, combination of underlines /// </Summary> /// <Param name = "Source"> </param> /// <returns> </returns> Public static bool isnormalchar (string source) {return RegEx. ismatch (source, @ "[\ W \ D _] +", regexoptions. ignorecase) ;}# 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.