C# 版 表單驗證

來源:互聯網
上載者:User

using System;
using System.Collections.Generic;
using System.Text;

namespace bookan.others
{
    class vli
    {
        /// <summary>
        /// 判斷是否被未空
        /// </summary>
        /// <returns>為空白返回false</returns>
        public static bool IsNull(string syllable)
        {
            if (syllable.Trim() == "") //判斷是否為空白輸入
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// 判斷非特殊字元
        /// </summary>
        /// <param name="syllable">驗證字串</param>
        /// <returns>為空白返回false</returns>
        public static bool IsEspecial(string syllable)
        {
            string regex = "[\u4e00-\u9fa5]";

            return RegexOperation(syllable, regex);

        }

        /// <summary>
        /// 非數字
        /// </summary>
        /// <param name="syllable">驗證字串</param>
        /// <returns>為空白返回false</returns>
        public static bool IsNumber(string syllable)
        {
            string regex = "^[0-9]{1,}$";
            //   string regex =@"^[1-9]$|^1[0-2]$";
            return RegexOperation(syllable, regex);
        }

        /// <summary>
        /// 名稱長度(20/50/100)
        /// </summary>
        /// <param name="syllable">驗證字串</param>
        /// <returns>為空白返回false</returns>
        public static bool CheckNameLength(string syllable, int Long)
        {
            switch (Long)
            {
                case 20:
                    if (syllable.Length >= 20) return false;
                    break;
                case 50:
                    if (syllable.Length >= 50) return false;
                    break;
                case 100:
                    if (syllable.Length >= 100) return false;
                    break;
            }

            return true;
        }

        #region 非英文字元
        /// <summary>
        /// 非英文字元
        /// </summary>
        /// <param name="syllable">驗證字串</param>
        /// <returns>驗證結果</returns>
        public static bool IsWorld(string syllable)
        {
            string regex = "^[A-Za-z]+$";

            return RegexOperation(syllable, regex);
        }
        #endregion

        /// <summary>
        /// 驗證字串是否合法
        /// </summary>
        /// <param name="syllable">需驗證字串</param>
        /// <param name="regex">Regex</param>
        /// <returns></returns>
        private static bool RegexOperation(string syllable, string regex)
        {
            //Regex的枚舉類型
            System.Text.RegularExpressions.RegexOptions options = (
                (System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace |
                System.Text.RegularExpressions.RegexOptions.Multiline) |
                System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            //載入Regex到枚舉類型上
            System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(regex, options);

            //返回驗證結果
            return reg.IsMatch(syllable);
        }

        /// <summary>
        /// 判斷密碼是否超出長度(6~16)
        /// </summary>
        /// <returns>超長返回false</returns>
        public static bool IsLong(string syllable)
        {
            if (!IsNull(syllable)) //判斷是否為空白輸入
            {
                return false;
            }

            bool problem = true;

            if (syllable.Length > 16 || syllable.Length < 6) //判斷是否符合密碼位元
            {
                return problem = false;
            }
            return problem;
        }

        /// <summary>
        /// 年齡(長度/整數)(1~110)
        /// </summary>
        /// <returns>不正確返回false</returns>
        public static bool IsAge(string syllable)
        {
            if (!IsNull(syllable)) //判斷是否為空白輸入
            {
                return false;
            }

            if ((Convert.ToInt32(syllable) > 110 || Convert.ToInt32(syllable) < 1))//判斷長度
            {
                return false;
            }

            string regex = "^[0-9]{1,}$"; //判斷為數字

            return RegexOperation(syllable, regex);
        }

        /// <summary>
        /// 是否符合電話號碼的要求(3-8,4-7,11)
        /// </summary>
        /// <returns>不符合返回false</returns>
        public static bool IsPhone(string syllable)
        {
            //判斷是否為空白輸入
            if (!IsNull(syllable))
            {
                return false;
            }

            //Regex
            string regex = @"\d{3}-\d{8}|\d{4}-\d{7}|\d{11}";

            //返回驗證結果
            return RegexOperation(syllable, regex);
        }

        /// <summary>
        /// 是否符合日期的要求(2002-02-02)
        /// </summary>
        /// <returns>不符合返回false</returns>
        public static bool IsDay(string syllable)
        {
            //判斷是否為空白輸入
            if (!IsNull(syllable))
            {
                return false;
            }

            //Regex
            string regex = @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$
";

            //返回驗證結果
            return RegexOperation(syllable, regex);
        }

        /// <summary>
        /// 是否符合電子郵件的要求
        /// </summary>
        /// <returns>不符合返回false</returns>
        public static bool IsE_Mail(string syllable)
        {
            //判斷是否為空白輸入
            if (!IsNull(syllable))
            {
                return false;
            }

            //Regex
            string regex = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";

            //返回驗證結果
            return RegexOperation(syllable, regex);
        }

        /// <summary>
        /// Regex字串
        /// </summary>
        /// <param name="strVerify"></param>
        /// <returns></returns>
        public static bool StrVerify(string syllable)
        {
            string regex = "[0-9]+$";//由數字和26個英文字母組成的字串
            return RegexOperation(syllable, regex);
        }

 

        /// <summary>
        /// Regex字串
        /// </summary>
        /// <param name="strVerify"></param>
        /// <returns></returns>
        public static bool Str(string syllable)
        {
            string regex = @"^\d+$";//只能輸入數字
            return RegexOperation(syllable, regex);
        }
  
    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.