C # validating data validation classes

Source: Internet
Author: User
Tags email string

Using system;using system.text;using system.text.regularexpressions;namespace dachie.common{///<summary>/// A summary description of the Common. </summary>public class Regexcomm{//private const string reg_date = @ "^ (\d{2}|\d{4}) [\-\/] ((0?[ 1-9]) | (1[0-2])) [\-\/] ((0? [1-9]) | ((1|2) [0-9]) |30|31) $ "; Private Const string reg_date = @ "^ (\d{2}|\d{4}) ((0[1-9]) | ( 1[0-2]) ((0[1-9)) | ( (1|2) [0-9]) | 30|31) $ ";p rivate const string reg_phone = @" ^ ((0[0-9]{2,3}) {0,1} ([0-9]{7,8})) $ ";p rivate const string reg_email = @" ^ ([\w -\.] +) @ ((\[[0-9]{1,3}\. [0-9] {1,3}\. [0-9] {1,3}\.) | ([\w-]+\.) +)) ([a-za-z]{2,4}| [0-9] {1,3}) (\]?) $ ";p rivate const string reg_mobile = @" (^0{0,1}13[0-9]{9}$) ";p rivate const string reg_idcard = @" ^ ([0-9]{14}|[ 0-9]{17}) (x|[ 0-9]) {1}$ ";p rivate const string reg_time = @" ^ (([0-1]?[ 0-9]) | (2[0-3])) ([\:]) ([0-5][0-9])) $ "; #region half-width verification///<summary>///half-angle verification///</summary>///<param name=" str "> Validated string </param>/// <returns></returns>public static bool Isdbc (String str) {Utf8enCoding encoding = new utf8encoding (); int byteCount = encoding. GetByteCount (str); int strLen = str. Length;if (StrLen = = ByteCount) {return true;} return false;} #endregion #region Full-width verification//<summary>///full-width verification///</summary>///<param name= "str" > Validated string </param >///<returns></returns>public static bool ISSBC (String str) {utf8encoding encoding = new UTF8Encoding (); int byteCount = encoding. GetByteCount (str); int strLen = str. Length;if (ByteCount = = StrLen * 3) {return true;} return false;} #endregion #region Date String validation///<summary>///Date string validation///</summary>///<param name= "Date" > Date string </param>///<returns> Valid: True, otherwise: false</returns>public static bool Isvaliddate (string date) {R Eturn Regex.IsMatch (date, regexcomm.reg_date);} #endregion #region Email validation//<summary>///Email validation///</summary>///<param name= "Email" > Email string </param>///<returns> valid: True, otherwise: false</returns>public static bool IsVAlidemail (string email) {return Regex.IsMatch (email, regexcomm.reg_email);} #endregion #region Phone Number validation///<summary>///phone number validation///</summary>///<param name= "Phone" > Phone number string            </param>///<returns> Valid: True, otherwise: false</returns>public static bool Isvaildphone (string phone) { return Regex.IsMatch (phone, regexcomm.reg_phone);} #endregion #region Mobile Number validation///<summary>///Mobile number validation///</summary>///<param name= "mobile" > Cell phone number string </param>///<returns> valid: True, otherwise: false</returns>public static bool Isvalidmobile (string Mobile) {return Regex.IsMatch (mobile,reg_mobile);} #endregion #region ID Number validation///<summary>///ID number validation///</summary>///<param name= "Idcard" > ID number string </param>///<returns> valid: True, otherwise: false</returns>public static bool Isvalididcard (string Idcard) {return Regex.IsMatch (Idcard, Regexcomm.reg_idcard);} #endregion #region Date string into a Date object///<summary>///DateStrings converted to date objects///</summary>///<param name= "date" > Date strings </param>///<returns> Date Objects </returns >public static DateTime Castdatetime (string date) {StringBuilder builder = new StringBuilder (); builder. Append (date. Substring (0,4)); builder. Append ("-"); builder. Append (date. Substring (4,2)); builder. Append ("-"); builder. Append (date. Substring (6,2)); return Convert.todatetime (builder. ToString ());} #endregion #region Date object into a date string///<summary>///Date object into a date string///</summary>///<param name= "Date" > Date Object </param>///<returns> date string </returns>public static string Castdatetime (DateTime date) {string Strdate = date. ToString ("Yyyy-mm-dd"); strdate = Strdate.replace ("-", ""); return strdate;} #endregion #region Time Format Validation///<summary>///Time format verification///</summary>///<param name= "Time" > String </ param>///<returns> Correct: True, error: false</returns>public static bool Isvalidtime (string time) {return Regex.IsMatch (time,reg_time);} #endregion}}

C # Validate data validation class

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.