2011-09-02[C#中使用正則表達式驗證]

 

驗證電話號碼的主要代碼如下:

public bool IsTelephone(string str_telephone)

        {

            return System.Text.RegularExpressions.Regex.IsMatch(str_telephone, @"^(\d{3,4}-)?\d{6,8}$");

        }

驗證手機號碼的主要代碼如下:

public bool IsHandset(string str_handset)

        {

     return System.Text.RegularExpressions.Regex.IsMatch(str_handset, @"^[1]+[3,5]+\d{9}");

        }

驗證身份證號的主要代碼如下:

          public bool IsIDcard(string str_idcard)

        {

            return System.Text.RegularExpressions.Regex.IsMatch(str_idcard, @"(^\d{18}$)|(^\d{15}$)");

        }

驗證輸入爲數字的主要代碼如下:

          public bool IsNumber(string str_number)

        {

            return System.Text.RegularExpressions.Regex.IsMatch(str_number, @"^[0-9]*$");

        }

驗證郵編的主要代碼如下:

          public bool IsPostalcode(string str_postalcode)

        {

     return System.Text.RegularExpressions.Regex.IsMatch(str_postalcode, @"^\d{6}$");

        }

        /// <summary>
        /// 驗證暱稱長度
        /// </summary>
        /// <param name='str'>驗證字符</param>
        /// <returns>bool</returns>
        public static bool CheckNickName(string str)
        {
            if (IsAllSpace(str))
            {
                return false;
            }
            else if (str.Length < 4 || str.Length > 18)
            {
                //if (IsChineseChar(str))
                //{
                    return true;
                //}
                //else
                //{
                //    return false;
                //}
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 驗證用戶名規則
        /// </summary>
        /// <param name='str'>驗證字符</param>
        /// <returns>bool</returns>
        public static bool CheckUsername(string str)
        {
            if (IsAllSpace(str))
            {
                return false;
            }
            else if ((str.Length < 4 || str.Length > 18 || !IsWordAndDigital(str.Substring(0, 1))))
            {
                if (IsChineseChar(str)) {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else if (!CheckPattern(str, @"^[A-Za-z0-9._\-]*$"))
            {
                if (IsChineseChar(str)) {
                    return true;
                }
                else
                {
                    return false;
                }
            }

            else
            {
                return true;
            }
        }

        /// <summary>
        /// 驗證密碼規則
        /// </summary>
        /// <param name='str'>驗證字符</param>
        /// <returns>bool</returns>
        public static bool CheckPassword(string str)
        {
            if (IsAllSpace(str))
            {
                return false;
            }
            else if (str.Length < 6 || str.Length > 18)
            {
                return false;
            }
            else if (!CheckPattern(str, @"^[A-Za-z0-9._\-]*$"))
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 驗證email規則
        /// </summary>
        /// <param name='str'>驗證字符</param>
        /// <returns>bool</returns>
        public static bool CheckEmail(string str)
        {
            if (IsAllSpace(str))
            {
                return false;
            }
            else if (!IsEmail(str))
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 驗證是否爲中文字符
        /// </summary>
        /// <param name='str'>驗證字符</param>
        /// <returns>bool</returns>
        public static bool CheckChinese(string str)
        {
            if (IsAllSpace(str))
            {
                return false;
            }
            else if (!IsChineseChar(str))
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 驗證特殊正則表達式
        /// </summary>
        /// <param name='str'>驗證字符</param>
        /// <param name='pattern'>正則表達式</param>
        /// <returns>bool</returns>
        public static bool CheckPattern(string str, string pattern)
        {
            if (IsAllSpace(str))
            {
                return false;
            }
            else if (!IsPattern(str, pattern))
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 驗證字符長度
        /// </summary>
        /// <param name='str'>驗證字符</param>
        /// <param name='min_lng'>最小長度</param>
        /// <param name='max_lng'>最大長度</param>
        /// <returns>bool</returns>
        public static bool CheckLength(string str, int min_lng, int max_lng)
        {
            if (IsAllSpace(str))
            {
                return false;
            }
            else if (str.Length > min_lng && str.Length < max_lng)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 驗證郵件
        /// </summary>
        /// <param name='str'>驗證字符</param>
        /// <returns>bool</returns>
        private static bool IsEmail(string str)
        {
            string stringPattern = @"^\w+([-+\.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$";
            bool match = Regex.IsMatch(str, stringPattern);
            return match;
        }

        /// <summary>
        /// 驗證數字和英文單詞
        /// </summary>
        /// <param name='str'>驗證字符</param>
        /// <returns>bool</returns>
        private static bool IsWordAndDigital(string str)
        {
            string stringPattern = @"^[A-Za-z0-9]*$";
            bool match = Regex.IsMatch(str, stringPattern);
            return match;
        }

        /// <summary>
        /// 驗證中文字符
        /// </summary>
        /// <param name='str'>驗證字符</param>
        /// <returns>bool</returns>
        private static bool IsChineseChar(string str)
        {
            string stringPattern = @"^([\u4E00-\u9FA5]|[\uFE30-\uFFA0])*$";
            bool match = Regex.IsMatch(str, stringPattern);
            return match;
        }

        /// <summary>
        /// 驗證是否爲空
        /// </summary>
        /// <param name='str'>驗證字符</param>
        /// <returns>bool</returns>
        private static bool IsAllSpace(string str)
        {
            string stringPattern = @"^\s*$";
            bool match = Regex.IsMatch(str, stringPattern);
            return match;
        }

        /// <summary>
        /// 驗證正則表達式
        /// </summary>
        /// <param name='str'>驗證字符</param>
        /// <param name='pattern'>正則表達式</param>
        /// <returns>bool</returns>
        private static bool IsPattern(string str, string pattern)
        {
            bool match = Regex.IsMatch(str, pattern);
            return match;
        }

        //數字的字符列表
        public static string numbers
        {
            get { return "0123456789"; }
        }

        /// <summary>
        /// 獲取數字
        /// </summary>
        /// <param name='o'>數字原型</param>
        /// <param name='returnValue'>默認值</param>
        /// <returns>int</returns>
        public static int IsNumeric(object o, int returnValue)
        {
            string s = (string)(o);
            string v = "";
            string l = numbers;
            for (int i = 0; i < o.ToString().Length; i++)
            {
                string single = s.Substring(i, 1);
                if (l.IndexOf(single) != -1)
                {
                    v += single;
                }
            }
            if (v == "")
                return returnValue;
            return int.Parse(v);
        }

        /// <summary>
        /// 驗證IP地址
        /// </summary>
        /// <param name='ip'>驗證字符</param>
        /// <returns>bool</returns>
        public static bool IsIP(string ip)
        {
            return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
        }

        /// <summary>
        /// 驗證數字
        /// </summary>
        /// <param name='strNumber'>驗證字符</param>
        /// <returns>bool</returns>
        public static bool IsNumber(string strNumber)
        {
            return new Regex(@"^([0-9])[0-9]*(\.\w*)?$").IsMatch(strNumber);
        }

        /// <summary>
        /// 驗證時間
        /// </summary>
        /// <param name='timeval'>驗證字符</param>
        /// <returns>bool</returns>
        public static bool IsTime(string timeval)
        {
            return Regex.IsMatch(timeval, "^((([0-1]?[0-9])|(2[0-3])):([0-5]?[0-9])(:[0-5]?[0-9])?)$");
        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章