C# 正则表达式

不全,但是都测试过。可用。

 

//验证email
public static bool isEmail(string inputEmail)
    {
        
string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
        Regex re 
= new Regex(strRegex);
        
if (re.IsMatch(inputEmail))
        {
            
return true;
        }
        
else
        {
            
return false;
        }
    }

//用户名 如果要控制长度,length设置一下
    public static bool username(string inputUsername)
    {

        
string strRegex = @"[a-zA-Z][a-zA-Z0-9_]*";
        Regex re 
= new Regex(strRegex);
        
if (re.IsMatch(inputUsername))
        {
            
return true;
        }
        
else
        {
            
return false;
        }
    }

//密码 如果要控制长度,length设置一下
    public static bool IsValidPassword(string inputPwd)
    {
        
string strRegex = @"^[^\s]{6,20}$";
        Regex re 
= new Regex(strRegex);
        
if (re.IsMatch(inputPwd))
        {
            
return true;
        }
        
else
        {
            
return false;
        }
    }

//是否是手机电话号码
    public static bool isPhone(string inputPhone)
    {
        
string s = @"^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\d{8}$";
        
if (Regex.IsMatch(inputPhone, s))
        {
            
return true;
        }
        
else
        {
            
return false;
        }
    }

 

 

posted @ 2010-03-17 14:00  兽王归来  阅读(259)  评论(1编辑  收藏  举报