C # common verification,

Source: Internet
Author: User

C # common verification,

Using System; using System. collections. generic; using System. linq; using System. text; using System. web; using System. web. UI. htmlControls; using System. web. UI; using System. text. regularExpressions; namespace Common {public class Validate {private static readonly Regex RegPhone = new Regex ("(^ (\ d {11 }) $ | ^ (\ d {7,8}) | (\ d {4} | \ d {3})-(\ d {7,8 }) | (\ d {4} | \ d {3})-(\ d {7, 8 }) -(\ d {4} | \ d {3} | \ d {2} | \ d {1}) | (\ d {7, 8 })-(\ D {4} | \ d {3} | \ d {2} | \ d {1}) $ )"); // phone number and phone verification private static Regex RegEmail = new Regex ("^ \ s * ([A-Za-z0-9 _-] + (\\. \ w +) * @ ([\ w-] + \\.) + \ w {2, 3}) \ s * $ "); // new Regex ("^ [\ w-] + @ [\ w-] + \\. (com | net | org | edu | mil | TV | biz | info) $ "); // a string of w English letters or numbers, like the [a-zA-Z0-9] syntax, private static Regex RegNum = new Regex ("^ [0-9] + $"); // must be a numeric Regular Expression (decimal or integer) private static Regex regex = new Regex (@ "/^ [0-9] + \.? [0-9] {0, 3} $ /"); /// <summary> // ID card positive expression // </summary> private static readonly Regex RegCardId = new Regex ("(^ \ d {15} $) | (^ \ d {17} ([0-9] | X | x) $ )"); # region determine whether the user input is legal // <summary> // determine whether the user input is legal // </summary> /// <param name = "text"> User input string </param> /// <param name = "maxLength"> maximum string Length </param> /// <returns> </returns> public static string InputText (string text, int maxLength) {if (string. isNullOrEm Pty (text) return string. Empty; text = text. Trim (); if (maxLength! = 0) if (text. length> maxLength) text = text. substring (0, maxLength); text = Regex. replace (text, "[\ s] {2,}", ""); text = Regex. replace (text, "(<[B | B] [r | R]/*>) + | (<[p | P] (. | \ n) *?>) "," \ N "); text = Regex. replace (text, "(\ s * & [n | N] [B | B] [s | S] [p | P]; \ s *) + ", ""); // text = Regex. replace (text, "<(. | \ n) *?> ", String. empty); // mask the label text = text. replace ("'", "'' "); return text ;} # endregion # region verify the phone number // check the phone number and phone number /// <summary> // check the phone number and phone number /// </summary> /// <param name = "inputData"> phone number or mobile phone number </param> // <returns> matching result </returns> public static bool IsPhone (string inputData) {Match m = RegPhone. match (inputData); return m. success ;} # endregion # Check whether the region parameter is a Chinese character /// <summary> /// check whether the parameter is a Chinese character /// </summary> /// <param name =" input "> input parameter </param> /// <returns> </returns> public static bool IsChinese (string input) {Regex regex = new Regex (@ "[\ u4e00-\ u9fa5]", RegexOptions. ignoreCase); return regex. isMatch (input );} # endregion # region email address // <summary> // verify the email address /// </summary> /// <param name = "inputData"> input string </ param> // <returns> Verification Result </returns> public static bool IsEmail (string inputData) {Match m = RegEmail. match (inputData); return m. success ;} # endregion # Whether region is a number /// <summary> /// whether it is a number /// </summary> /// <param name = "inputData"> input string </param> // <returns> whether it is a number </returns> public static bool IsNum (string inputData) {if (string. isNullOrEmpty (inputData) {return false;} Match m = RegNum. match (inputData); return m. success ;} /// <summary> /// judge whether it is an integer or a decimal number. /// </summary> /// <param name = "str"> input string </param>/ // <returns> whether it is a number </returns> public static bool IsNumAll (string str) {if (string. isNullOrEmpty (str) {return false;} Match m = regex. match (str); return m. success ;} # endregion # Whether region is an ID card // <summary> // whether it is an ID card // </summary> /// <param name = "inputData"> input string </param> /// <returns> ID card </returns> public static bool IsCardId (string inputData) {Match m = RegCardId. match (inputData); return m. success ;} # endregion // <summary> // determine whether the string is a pure number. // </summary> // <param name = "message"> source string </param> /// <returns> </returns> public static bool IsNumberic (string message) //, out int result {System. text. regularExpressions. regex rex = new System. text. regularExpressions. regex (@ "^ \ d + $"); var result =-1; if (rex. isMatch (message) {result = int. parse (message); return true;} else return false ;}}}

 

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.