首页 > 技术文章 > 正则表达式验证

llxy 2014-09-15 15:36 原文

 public class RegularHelp
    {

        //验证Email地址
        public static bool IsValidEmail(string strIn)
        {
            // Return true if strIn is in valid e-mail format.
            return Regex.IsMatch(strIn, @"^([\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 IsValidTel(string strIn)
        {
            return Regex.IsMatch(strIn, @"(d+-)?(d{4}-?d{7}|d{3}-?d{8}|^d{7,8})(-d+)?");
        }

        //验证手机号
        public static bool IsValidPhone(string strIn)
        {
            return Regex.IsMatch(strIn, @"^[1][345789]\d{9}$");
        }

        //验证登录名
        public static bool IsVaildLoginName(string strIn)
        {
            return Regex.IsMatch(strIn, @"^[a-zA-Z]{1}[a-zA-Z0-9_]{5,15}$");
        }

        //验证密码
        public static bool IsVaildPassword(string strIn)
        {
            return Regex.IsMatch(strIn, @"^[A-Za-z0-9!@#$%^&*~]{6,22}$");

        }
        //验证身份证15位与18位
        public static bool IsVaildPersonCard(string strIn)
        {
            return Regex.IsMatch(strIn, @"^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$");
        }

        //验证邮编
        public static bool IsVaildPostalcode(string strIn)
        {
            return Regex.IsMatch(strIn, @"^[1-9]\d{5}(?!\d)$");
        }

    }

 

推荐阅读