Client verification method:
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
This. Button1.Attributes. Add ("onclick", "return checkSame ()"); // Add an onclick () event to Button1, and the Button is the Server Control
}
}
<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.exe c (ob1) {alert ("bu pi pei"); return false ;}
Return true;
}
</Script>
Server Verification Method: using System. Text. RegularExpressions;
Protected void button#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 ('user name cannot be blank! '); </Script> ");}
Regex reg = new Regex (@ "[a-z0-9 _ \ u4e00-\ u9fa5 }");
If (! Reg. IsMatch (username ))
{
Label2.visible = true;
Label2.text = "<font color = Red> the user name is unavailable </font> ";
Return;
}
}