1.验证是否为整数

 

private bool IsNumber(string num)
        {
            if (string.IsNullOrEmpty(num))
            {
                return false;
            }
           
            if (num == "0")
            {
                return true;
            }

            Regex rg = new Regex("^[0-9]*[1-9][0-9]*$");

            if (rg.IsMatch(num))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

2. 验证是否为有效IP

private bool IsValidIP(string ipAddress)
        {     
            string pattern = @"^(([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5])))\.)(([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5])))\.){2}([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5])))$";

            Regex rg = new Regex(pattern);

            if (rg.IsMatch(ipAddress))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

3.验证是否为有效FQDN

 

 private bool isFQDN(string address)
        {
            if (string.IsNullOrEmpty(address))
            {
                return false;
            }

            string pattern = @"(?=^.{1,254}$)(^(?:(?!\d+\.)[a-zA-Z0-9_\-]{1,63}\.?)+(?:[a-zA-Z]{1,})$)";
            Regex rg = new Regex(pattern);

            if (rg.IsMatch(address))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

 

posted on 2012-05-23 15:24  higirle  阅读(210)  评论(0)    收藏  举报