In web development, we always want to achieve the same functionality, and the fewer clients and servers interact, the better. This can not only improve the user's browsing progress, but also reduce the pressure on the server, now in the web development of the "login module" design as an example, to achieve the client to verify the length of the account password compliance requirements, meet the requirements, the service-side program further verification.
Process: 1. User Input account password
2. Client-side JavaScript code validates the length of characters in a TextBox
3. If the length does not meet the requirements, do not transfer to the execution server program
4. If the length meets the requirements, then execute the server program to compare the information with the information in the database.
The above is the process of program execution, readers can continue to look at the code analysis, or click here to download the source code for their own research.
First we add two text boxes, a button, an interface to the Login.aspx (front desk)
Front Code:
<%@ page language= "C #" autoeventwireup= "true" codefile= "Login.aspx.cs" inherits= "Login"%><! DOCTYPE html>#box {border:2px solid #00ffff; width:250px; margin-left:auto; Margin-right:auto; margin-top:100px; padding:10px;} #bt_commit {margin-left:150px;} </style>Account Number:<asp:textbox id= "TXT_USR" runat= "server" ></asp:textbox><br/><br/> Password: <asp:TextBox ID= " Txt_pwd "runat=" Server "textmode=" Password "></asp:textbox><br/><br/> <asp:button ID=" Bt_ Commit "runat=" server "text=" login "/> </div> </form></body> Background code: (No custom code has been added yet)
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls; Public Partial classlogin:system.web.ui.page{protected voidPage_Load (Objectsender, EventArgs e) { } protected voidBt_commit_click (Objectsender, EventArgs e) { }}
Then add the code after the foreground and behind the scenes as follows:
Front desk:
<%@ page language= "C #" autoeventwireup= "true" codefile= "Login.aspx.cs" inherits= "Login"%><! DOCTYPE html>#box {border:2px solid #00ffff; width:250px; margin-left:auto; Margin-right:auto; margin-top:100px; padding:10px;} #bt_commit {margin-left:150px;} </style><script type= "Text/javascript" > Function Yanzheng () {var usr_len = Document.getele Mentbyid ("Txt_usr"). Value; var Pwd_len = document.getElementById ("Txt_pwd"). Value; if (usr_len.length<1) {alert ("Please enter your account!") "); return false; } else if (pwd_len.length<6| | PWD_LEN.LENGTH>20) {alert ("Password length is not legal! "); return false; } else {return true; }} </script>Account Number:<asp:textbox id= "TXT_USR" runat= "server" ></asp:textbox><br/><br/> Password: <asp:TextBox ID= " Txt_pwd "runat=" Server "textmode=" Password "></asp:textbox><br/><br/> <asp:button ID=" Bt_c Ommit "runat=" server "text=" login "onclick=" Bt_commit_click "/> </div> </form></body>Background:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls; Public Partial classlogin:system.web.ui.page{protected voidPage_Load (Objectsender, EventArgs e) { if (! IsPostBack) {Bt_commit. Attributes.Add ("OnClick", "Return Yanzheng ()"); } } protected voidBt_commit_click (Objectsender, EventArgs e) { Page.ClientScript.RegisterStartupScript ( typeof (Page), "", "<script>alert (' Client authentication passed! ');</script> "); }}
This enables the server-side (background) code to be executed only if the client (foreground) validation passes. Click here to download the source code.
Run results
Asp. NET implementation of content foreground validation background processing