C # Validation classes (using regular expressions to validate text boxes)

Source: Internet
Author: User
Tags valid email address

Using system;using System.Text.RegularExpressions;     namespace sg_vqcdatacollection{///<summary>////////////through the Regex class in the Framwork class library, some special function data checks/         public class Metarnetregex {private static Metarnetregex instance = NULL;                 public static Metarnetregex getinstance () {if (metarnetregex.instance = = null) {             Metarnetregex.instance = new Metarnetregex ();         } return metarnetregex.instance; } private Metarnetregex () {}///<summary>///////&L///input string only contains kanji// t;/summary>//<param name= "input" ></param>///<returns></returns> PU            Blic static bool Ischinesech (string input) {//regex regex = new Regex ("^[\ one-\ calls]+$");             Changed the regex regex = new Regex ("^[\ a-\ calls]+$"); return regex.         IsMatch (input); }//<summaRy>////to match 3-bit or 4-bit area code numbers, where the area code can be enclosed in parentheses, or/or not, the area code and local number can be used with hyphens or spaces,/////////(0 \d{2}\) [-]?\d{8}|0\d{2}[-]?\d{8}|\ (0\d{3}\) [-]?\d{7}|0\d{3}[-]?\d{7}//</summary>//<para M name= "input" ></param>//<returns></returns> public static bool Isphone (String INP UT) {string pattern = "^\\ (0\\d{2}\\) [-]?\\d{8}$|^0\\d{2}[-]?\\d{8}$|^\\ (0\\d{3}\\) [-]?\\d{7}$|^0\             \d{3}[-]?\\d{7}$ ";             Regex regex = new regex (pattern); return regex.         IsMatch (input); }///<summary>///Determine if the string entered is a valid mobile phone number///</summary>//<param name= "INP         UT "></param>///<returns></returns> public static bool Ismobilephone (string input)             {Regex regex = new Regex ("^13\\d{9}$"); return regex.         IsMatch (input); }//<summary>//judgmentThe input string contains only numbers///can match integers and floating point numbers///^-?\d+$|^ (-?\d+) (\.\d+)? $//</summary>//<para M name= "input" ></param>//<returns></returns> public static bool Isnumber (string in             Put) {string pattern = "^-?\\d+$|^ (-?\\d+) (\\.\\d+)? $";             Regex regex = new regex (pattern); return regex.         IsMatch (input); }//<summary>///Match non-negative integer//////</summary>//<param name= "Inpu         T "></param>///<returns></returns> public static bool Isnotnagtive (string input)             {Regex regex = new Regex (@ "^\d+$"); return regex.         IsMatch (input); }///<summary>///matching positive integers///</summary>//<param name= "input" ></pa      ram>//<returns></returns> public static bool Isuint (string input) {       Regex regex = new Regex ("^[0-9]*[1-9][0-9]*$"); return regex.         IsMatch (input); }///<summary>///The input string Word contains the English alphabet///</summary>//<param name= "input" &         gt;</param>//<returns></returns> public static bool Isenglisch (string input)             {Regex regex = new Regex ("^[a-za-z]+$"); return regex.         IsMatch (input); }///<summary>///To determine if the string entered is a valid email address///</summary>//<param name=         "Input" ></param>///<returns></returns> public static bool Isemail (string input) {String pattern = @ "^ ([\w-\.] +) @ ((\[[0-9]{1,3}\. [0-9] {1,3}\. [0-9] {1,3}\.) | ([\w-]+\.) +)) ([a-za-z]{2,4}| [0-9] {1,3}) (\]?)             $";             Regex regex = new regex (pattern); return regex.         IsMatch (input);       }///<summary>///Determine if the input string contains only numbers and English letters  </summary>//<param name= "input" ></param>//<returns></returns>             public static bool Isnumandench (string input) {string pattern = @ "^[a-za-z0-9]+$";             Regex regex = new regex (pattern); return regex.         IsMatch (input); }///<summary>///Determine if the string entered is a hyperlink///</summary>//<param name= "input"             ></param>///<returns></returns> public static bool Isurl (string input) { String pattern = @ "http://([\w-]+\.)             +[\w-]+ (/[\w-./?%&=]*)? "; String pattern = @ "^[a-za-z]+://(\w+ (-\w+) *) (\) (             \w+ (-\w+) *) * (\?\s*)? $ ";             Regex regex = new regex (pattern); return regex.         IsMatch (input); }///<summary>///Determines whether the input string represents an IP address///</summary>//<param name= "INP UT "> string to be compared </param>//<retUrns> is the IP address of true</returns> public static bool IsIPv4 (string input) {string[] IPs = i Nput.             Split ('. ');             Regex regex = new Regex (@ "^\d+$"); for (int i = 0; i < ips.length; i++) {if (!regex.                 IsMatch (Ips[i])) {return false;                 } if (Convert.touint16 (ips[i]) > 255) {return false;         }} return true; }///<summary>///To calculate the character length of a string, a kanji character will be calculated as two characters////</summary>//<param NA Me= "Input" > string to be computed </param>//<returns> length of returned string </returns> public static int Getcoun T (string input) {return Regex.Replace (input, @ "[\-\ calls/g]", "AA").         Length; }///<summary>///Call regex in the IsMatch function to achieve generic regular expression matching///</summary>//<param name= "pattern" > the regular expression pattern to match. </param>//<param name= "Input" > string to search for matches </param>//<returns> If the regular expression finds a match, the T Rue; otherwise, false. </returns> public static bool IsMatch (string pattern, string input) {regex regex = new             Regex (pattern); return regex.         IsMatch (input);         }///<summary>///To replace all occurrences of the specified regular expression pattern with a replacement string, starting with the first character in the input string. </summary>//<param name= "pattern" > Pattern string </param>//<param name= "input" > input Word Strings </param>///<param name= "Replacement" > string to replace </param>//<returns> Returns the result of the substitution &L             t;/returns> public static string Replace (string pattern, string input, string replacement) {             Regex regex = new regex (pattern); return regex.         Replace (input, replacement);         }///<summary>///split the input string at the location defined by the regular expression pattern. </summary>//<param name= "pattern" > Pattern string </param>//<param name= "Input" > Input string </para         m>//<returns></returns> public static string[] Split (string pattern, string input)             {Regex regex = new regex (pattern); return regex.         Split (input); }///<summary>///Determine if the string entered is a valid IPV6 address///</summary>//<param name= "I         Nput "></param>///<returns></returns> public static bool IsIPV6 (string input)             {String pattern = "";             string temp = input; string[] STRs = temp.             Split (': '); if (STRs.             Length > 8) {return false;             } int count = Metarnetregex.getstringcount (Input, "::");             if (Count > 1) {return false;    } else if (count = = 0) {             Pattern = @ "^ ([\da-f]{1,4}:) {7}[\da-f]{1,4}$";                 Regex regex = new regex (pattern); return regex.             IsMatch (input);                 } else {pattern = @ "^ ([\da-f]{1,4}:) {0,5}::([\da-f]{1,4}:) {0,5}[\da-f]{1,4}$";                 Regex regex1 = new regex (pattern); Return REGEX1.             IsMatch (input); }}/* ******************************************************************* * 1, the number of strings seen by ":" To split the string         If the leader degree is less than or equal to 8 * 2, the input IPV6 string is judged to have "::".         * 3, if there is no ":" Using ^ ([\da-f]{1,4}:) {7}[\da-f]{1,4}$ to determine * 4, if there is ":", judging "::" whether to appear once * 5, if more than once return false * 6, ^ ([\da-f]{1,4}:) {0,5}::([\da-f]{1,4}:) {0,5}[\da-f]{1,4}$ * *************************************************         ////<summary>///Compare the number of occurrences of the string in the input string///</summary> <param name= "Input" > Source string </param>///<param name= "Compare" > string used for comparison </param>//<returns> string Compare the number of occurrences in the input string </returns&gt         ; private static int Getstringcount (string input, string compare) {int index = input.             IndexOf (Compare); if (Index! =-1) {return 1 + getstringcount (input. Substring (index + Compare).             Length), compare);             } else {return 0; }         }     } }

Reprinted from: http://blog.csdn.net/hwt0101/article/details/7788480

C # Validation classes (using regular expressions to validate text boxes)

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.