C# 經常使用驗證

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}))$)");  //電話號碼和手機驗證
        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)$");//w 英文字母或數字的字符串,和 [a-zA-Z0-9] 語法同樣
        private static Regex RegNum = new Regex("^[0-9]+$");

        //必須是數字正則表達式(小數或整數)
        private static Regex regex = new Regex(@"/^[0-9]+\.?[0-9]{0,3}$/");
        /// <summary>
        /// 身份證正值表達式
        /// </summary>
        private static readonly Regex RegCardId = new Regex("(^\\d{15}$)|(^\\d{17}([0-9]|X|x)$)");
        
        #region 肯定用戶輸入是否合法
        /// <summary>
        /// 肯定用戶輸入是否合法
        /// </summary>
        /// <param name="text">用戶輸入字符串</param>
        /// <param name="maxLength">最大字符串長度</param>
        /// <returns></returns>
        public static string InputText(string text, int maxLength)
        {
            if (string.IsNullOrEmpty(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); //屏蔽標籤
            text = text.Replace("'", "''");
            return text;
        }
        #endregion

        #region 驗證電話號碼
        // 電話號碼和手機號碼檢查
        /// <summary>
        /// 電話號碼和手機號碼檢查
        /// </summary>
        /// <param name="inputData">電話號碼或手機號碼</param>
        /// <returns>匹配結果</returns>
        public static bool IsPhone(string inputData)
        {
            Match m = RegPhone.Match(inputData);
            return m.Success;
        }
        #endregion

        #region 驗證參數是否爲中文字符
        /// <summary>
        /// 驗證參數是否爲中文字符
        /// </summary>
        /// <param name="input">輸入參數</param>
        /// <returns></returns>
        public static bool IsChinese(string input)
        {
            Regex regex = new Regex(@"[\u4e00-\u9fa5]", RegexOptions.IgnoreCase);
            return regex.IsMatch(input);
        }
        #endregion

        #region 郵件地址

        /// <summary>
        /// 郵件地址驗證
        /// </summary>
        /// <param name="inputData">輸入字符串</param>
        /// <returns>驗證結果</returns>
        public static bool IsEmail(string inputData)
        {
            Match m = RegEmail.Match(inputData);
            return m.Success;
        }
        #endregion

        #region 是否爲數字
        /// <summary>
        /// 是否爲數字
        /// </summary>
        /// <param name="inputData">輸入字符串</param>
        /// <returns>是否爲數字</returns>
        public static bool IsNum(string inputData)
        {
            if(string.IsNullOrEmpty(inputData))
            {
                return false;
            }
            Match m = RegNum.Match(inputData);
            return m.Success;
        }
        /// <summary>
        /// 判斷是不是整數或小數
        /// </summary>
        /// <param name="str">輸入的字符串</param>
        /// <returns>是否爲數字</returns>
        public static bool IsNumAll(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return false;
            }
            Match m = regex.Match(str);
            return m.Success;
        }

        #endregion

        #region 是否爲身份證
        /// <summary>
        /// 是否爲身份證
        /// </summary>
        /// <param name="inputData">輸入字符串</param>
        /// <returns>是否爲身份證</returns>
        public static bool IsCardId(string inputData)
        {
            Match m = RegCardId.Match(inputData);
            return m.Success;
        }
        #endregion


        /// <summary>
        /// 判斷字符串是不是純數字
        /// </summary>
        /// <param name="message">源字符串</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;
        }
    }
}
相關文章
相關標籤/搜索