C# 版 表单验证

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">正则表达式</param>
        /// <returns></returns>
        private static bool RegexOperation(string syllable, string regex)
        {
            //正则表达式的枚举类型
            System.Text.RegularExpressions.RegexOptions options = (
                (System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace |
                System.Text.RegularExpressions.RegexOptions.Multiline) |
                System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            //加载正则表达式到枚举类型上
            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;
            }

            //正则表达式
            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;
            }

            //正则表达式
            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;
            }

            //正则表达式
            string regex = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";

            //返回验证结果
            return RegexOperation(syllable, regex);
        }


        /// <summary>
        /// 正则表达式字符串
        /// </summary>
        /// <param name="strVerify"></param>
        /// <returns></returns>
        public static bool StrVerify(string syllable)
        {
            string regex = "[0-9]+$";//由数字和26个英文字母组成的字符串
            return RegexOperation(syllable, regex);
        }

 

        /// <summary>
        /// 正则表达式字符串
        /// </summary>
        /// <param name="strVerify"></param>
        /// <returns></returns>
        public static bool Str(string syllable)
        {
            string regex = @"^\d+$";//只能输入数字
            return RegexOperation(syllable, regex);
        }
  
    }
}

posted on 2008-03-18 14:50 top15from_old1 阅读(...) 评论(...) 编辑 收藏

导航

公告

随州简爱保罗定制衣柜专卖店为您准备了全套家装所需的定制家具,用工厂直销的价格和衣柜行业领导品牌的实力恭候您的光临。 随州市清河路清河丽景大门右侧18307228966 0722-3594778