Asp.net client or server Verification Method

Source: Internet
Author: User

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;
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.