日期:2014-05-19  浏览次数:20461 次

验证控件的问题~
我写了这么一个验证Email的函数
public   static   bool   IsEmail(string   str)
        {                
                if   (str.Trim()   ==   " "   ||   str   ==   null)
                {
                        return   false;
                }
                else
                {
                        Regex   re   =   new   Regex(@ "\s ");
                        str   =   re.Replace(str.Replace( "%20 ",   "   "),   "   ");
                        string   pattern   =   @ "\w+([-+. ']\w+)*[@#]\w+([-.]\w+)*\.\w+([-.]\w+)* ";
                        if   (Regex.IsMatch(str,   pattern))
                        {
                                return   true;
                        }
                        else
                        {
                                return   false;
                        }
                }
        }

然后页面上有一个验证控件,我填的正则表达式就是上面的\w+([-+. ']\w+)*[@#]\w+([-.]\w+)*\.\w+([-.]\w+)*


但是我发现,两者验证的结果不一样...有人知道是什么原因吗?

------解决方案--------------------
验证非空只是给你举个例子而已

你在你的正则前加上“^”,后面加上“$”,
string pattern = @ "^\w+([-+. ']\w+)*[@#]\w+([-.]\w+)*\.\w+([-.]\w+)*$ ";
这样再试下