常用的验证

来源:互联网 发布:顾家沙发 知乎 编辑:程序博客网 时间:2024/04/28 04:47
 


namespace UtilityValidator
{
    using System;
    using System.Text.RegularExpressions;

    public class Validators
    {
 //验证是否是有效日期
        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}$");
        }

    }
}

原创粉丝点击