C#驗證字串是否是數字,是否包含中文,是否是郵箱格式,是否是電話格式

來源:互聯網
上載者:User

標籤:c#驗證字串是否是數字   c#驗證字串是否包含中文   c#驗證字串是否是郵箱格式   c#驗證字串是否是電話格式   c#驗證字串   

    using System;
    using System.Web;
    using System.Text;
    using System.Web.UI.WebControls;
    using System.Text.RegularExpressions;

    public class ValidateHelper
    {
        private static Regex RegNumber = new Regex("^[0-9]+$");
        private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
        private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
        private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$");
        private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");
        private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");
        private static Regex RegTell = new Regex("^(([0-9]{3,4}-)|[0-9]{3.4}-)?[0-9]{7,8}$");
        private static Regex RegSend = new Regex("[0-9]{1}([0-9]+){5}");
        private static Regex RegUrl = new Regex("^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?|[a-zA-z]+://((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))$");
        private static Regex RegMobilePhone = new Regex("^13|15|18[0-9]{9}$");
        private static Regex RegMoney = new Regex("^[0-9]+|[0-9]+[.]?[0-9]+$");

        #region 數字字串檢查

        /// <summary>
        /// 是否數字字串
        /// </summary>
        /// <param name="inputData">輸入字串</param>
        public static bool IsNumber(string inputData)
        {
            if (!string.IsNullOrEmpty(inputData))
            {
                Match m = RegNumber.Match(inputData);
                return m.Success;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 是否數字字串 可帶加號或減號
        /// </summary>
        /// <param name="inputData">輸入字串</param>
        public static bool IsNumberSign(string inputData)
        {
            Match m = RegNumberSign.Match(inputData);
            return m.Success;
        }

        /// <summary>
        /// 是否是浮點數
        /// </summary>
        /// <param name="inputData">輸入字串</param>
        public static bool IsDecimal(string inputData)
        {
            Match m = RegDecimal.Match(inputData);
            return m.Success;
        }

        /// <summary>
        /// 是否是浮點數 可帶加號或減號
        /// </summary>
        /// <param name="inputData">輸入字串</param>
        public static bool IsDecimalSign(string inputData)
        {
            Match m = RegDecimalSign.Match(inputData);
            return m.Success;
        }

        #endregion

        #region 中文檢測

        /// <summary>
        /// 檢測是否有中文字元
        /// </summary>
        public static bool IsHasCHZN(string inputData)
        {
            Match m = RegCHZN.Match(inputData);
            return m.Success;
        }

        #endregion

        #region 郵件地址
        /// <summary>
        /// 是否是郵箱
        /// </summary>
        /// <param name="inputData">輸入字串</param>
        public static bool IsEmail(string inputData)
        {
            Match m = RegEmail.Match(inputData);
            return m.Success;
        }

        #endregion

        #region 電話,郵遞區號,網路地址,手機號碼,價格
        /// <summary>
        /// 驗證是否是電話
        /// </summary>
        public static bool IsPhone(string inputDate)
        {
            if (!string.IsNullOrEmpty(inputDate))
            {
                Match m = RegTell.Match(inputDate);
                return m.Success;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 是否是郵遞區號
        /// </summary> 
        public static bool IsSend(string inputDate)
        {
            if (!string.IsNullOrEmpty(inputDate))
            {
                Match m = RegSend.Match(inputDate);
                return m.Success;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 是否是網路地址
        /// </summary>
        public static bool IsUrl(string inputDate)
        {
            Match m = RegUrl.Match(inputDate);
            return m.Success;
        }
        /// <summary>
        /// 是否是手機號碼
        /// </summary>
        public static bool IsMobilePhone(string inputDate)
        {
            Match m = RegMobilePhone.Match(inputDate);
            return m.Success;
        }
        /// <summary>
        /// 是否是價格
        /// </summary>
        public static bool IsMoney(string inputDate)
        {
            Match m = RegMoney.Match(inputDate);
            return m.Success;
        }
        #endregion
        #region 是否是時間格式
        /// <summary>
        /// 判斷一個字串是否時間格式
        /// </summary>
        /// <param name="inputData">輸入字串</param>
        /// <returns></returns>
        public static bool IsDateTime(string inputData)
        {
            try
            {
                Convert.ToDateTime(inputData);
                return true;
            }
            catch
            {
                return false;
            }
        }

        #endregion
    }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.