客户端验证方式:

 

 protected void Page_Load(object sender, EventArgs e)
    {
       

        if (!IsPostBack)
        {
            this.Button1.Attributes.Add("onclick", "return checkSame()");//为Button1添加onclick()事件 ,Button为服务器控件
        }

 

   }

 <script type="text/javascript">
   
   function trim(s) {
          return s.replace( /^\s*/, "" ).replace( /\s*$/, "" );
   }
      
  function checkSame(){
      var ob1=trim(document.getElementById('username').value);
      var patrn=/^[A-Za-z0-9_-]+$/;
      if(ob1.length==0) {
      alert('wrong');
      return false; }
      if (!patrn.exec(ob1)) {alert("bu pi pei"); return   false ; }
      return true;
  
     
   }
   
    </script>

 

 

 

服务端验证方式:using System.Text.RegularExpressions;

                    protected void Button1_Click(object sender, EventArgs e)
    {  string username=this.username.Text;
       string pass = this.password.Text;
    
      
 
       if (username.Trim().Length == 0 || username == "") { RegisterStartupScript("", "<script language=javascript>alert('用户名不能为空!');</script>"); }
       Regex reg = new Regex(@"[a-z0-9_\u4e00-\u9fa5]{3,20}");
       if (!reg.IsMatch(username))
       {
           Label2.Visible = true;
            Label2.Text = "<font color=red>用户名不可用</font>";
           return;
       }

}

posted on 2009-04-13 16:22  releaseyou  阅读(572)  评论(0编辑  收藏  举报