我在这里: 首页 » 程序人生 » 浏览文章: 常用的类型验证[Regex]
« 色戒:一首简单的歌人生就是投资和收益的人生 »

常用的类型验证[Regex]

 //验证有效日期
        public static bool isValidDate(string strln)
        {
            if (Regex.IsMatch(strln, @"^[12]{1}(\d){3}[-][01]?(\d){1}[-][0123]?(\d){1}$"))
            {
                return (strln.CompareTo("1753-01-01") >= 0);
            }
            return false;
        }

 //验证Email
        public static bool isValidEmail(string strln)
        {
            return Regex.IsMatch(strln, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
        }

 //验证传真
        public static bool isValidFax(string strln)
        {
            return Regex.IsMatch(strln, @"^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$");
        }

 //验证移动电话号码
        public static bool isValidMobil(string strln)
        {
            return Regex.IsMatch(strln, @"^(\d)+[-]?(\d){6,12}$");
        }

 //验证是否只含有字母
        public static bool isValidOnllyChar(string strln)
        {
            return Regex.IsMatch(strln, "^[A-Za-z]+$");
        }

 //验证是否只含有汉字
        public static bool isValidOnllyChinese(string strln)
        {
            return Regex.IsMatch(strln, @"^[\u4e00-\u9fa5]+$");
        }

 //验证是否只含有数字
        public static bool isValidOnlyNumber(string strln)
        {
            return Regex.IsMatch(strln, "^[0-9]+$");
        }

 //验证是否是有效密码
        public static bool isValidPassWord(string strln)
        {
            return Regex.IsMatch(strln, @"^(\w){6,20}$");
        }

 //验证是否是有效电话号码
        public static bool isValidTel(string strln)
        {
            return Regex.IsMatch(strln, @"^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$");
        }

 //验证是否是有效邮编号码
        public static bool isValidZip(string strln)
        {
            return Regex.IsMatch(strln, "^[a-z0-9 ]{3,12}$");
        }

  • 相关文章

发表评论

为了防止SPAM,含链接的评论需要审核后才能显示。

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

支持我们

最新评论及回复

最近发表

Search