JS aspx Verification Code image; JS sets the form mouse style; JS gets all links on the page; JS onchange event, try throw catch for form verification; JS shields all images on the page;

Source: Internet
Author: User

1. Principle: Image src = ASPX page, which generates Verification Code Images

The verification code generated by aspx is stored in the cookie ["yanzhengma "].

Log on to the client and read the verification code from the cookie to check whether it is consistent with the entered one.

 

JSCode

 /*  Password verification trigger: click Login  */  Function  Checkusrpsw (){  VaR Username = Document. getelementbyid ("username" ). Value;  VaR Password = Document. getelementbyid ("password" ). Value; If (Document. getelementbyid ("username"). value = "") {alert ("username cannot be blank "); Return  ;}  If (Document. getelementbyid ("password"). value = "") {alert ("password cannot be blank "); Return  ;}  VaR Checkcode = getcookie ("yzmcode" );  If (Checkcode. tolowercase ()! = Document. getelementbyid ("inpcheckcode"). value. tolowercase () {alert ("Incorrect verification code "); Return  ;}  VaR Pajax = New  Ajax ();  VaR Url = encodeuri ("addedhandlers/login. ashx? Type = login & paras = "+ username +", "+ Password); pajax. Post (URL,  Null , False , Function  (Info ){  VaR Status = Info. responsetext;  If (Status! = "Success" ) {Alert (Status );} Else {Location. href = "index.htm" ;}});}  /*  Refresh Verification Code  */  Function  Changecheckcode (){  VaR I = Math. Random (); document. getelementbyid ( "Imgcheckcode"). src = "aspxpages/imagecode. aspx? I = "+ I; //  Refresh img  }  /*  Get cookie */  Function  Getcookie (c_name ){  If (Document. Cookie. length> 0 ) {C_start = Document. Cookie. indexof (c_name + "=" )  If (C_start! =-1 ) {C_start = C_start + c_name.length + 1 C_end = Document. Cookie. indexof (";" , C_start)  If (C_end =-1) c_end = Document. Cookie. Length Return  Unescape (document. Cookie. substring (c_start, c_end ))}}  Return "" } 

 

Html

 <  TD  > <  Input  Name  = ""  ID  = "Inpcheckcode"  Type  = "Text"  Class = "Text_bgs"   />              <  A  Href  = "#"  Class  = "Yzm_img"  >  <! --    -->                  <  IMG  SRC  = "Aspxpages/imagecode. aspx" ALT  = "Verification code"  ID  = "Imgcheckcode"  Onclick  = "Changecheckcode ()"  Title  = "Cannot see clearly? Click to change the verification code"  />              </  A  > </  TD  > 

 

Aspx code

Using system; using system. data; using system. configuration; using system. collections; using system. web; using system. web. security; using system. web. ui; using system. web. UI. webcontrols; using system. web. UI. webcontrols. webparts; using system. web. UI. htmlcontrols; using system. drawing. design; using system. drawing; public partial class _ default: system. web. UI. page {protected void page_load (Object sender, eventargs e) {This. createcheckcodeimage (rndnum ();} private string rndnum () {int number; char code; string checkcode = string. empty; system. random random = new random (); For (INT I = 0; I <4; I ++) {number = random. next (); If (Number % 2 = 0) code = (char) ('0' + (char) (Number % 10); else code = (char) ('A' + (char) (Number % 26); checkcode + = code. tostring ();} response. cookies. add (New httpcookie ("yzmcode", checkcode); Return checkcode;} private void createcheckcodeimage (string checkcode) {If (checkcode = NULL | checkcode. trim () = string. empty) return; system. drawing. bitmap image = new system. drawing. bitmap (INT) math. ceiling (checkcode. length * 12.5), 22); graphics G = graphics. fromimage (image); try {// generate random generator random = new random (); // clear the image background color G. clear (color. white); // specifies the background noise line of the image. For (INT I = 0; I <25; I ++) {int X1 = random. next (image. width); int X2 = random. next (image. width); int Y1 = random. next (image. height); int y2 = random. next (image. height); G. drawline (new pen (color. silver), X1, Y1, X2, Y2);} Font font = new system. drawing. font ("Arial", 12, (system. drawing. fontstyle. bold | system. drawing. fontstyle. italic); system. drawing. drawing2d. lineargradientbrush brush = new system. drawing. drawing2d. lineargradientbrush (New rectangle (0, 0, image. width, image. height), color. blue, color. darkred, 1.2f, true); G. drawstring (checkcode, Font, brush, 2, 2); // foreground noise of the image to be painted for (INT I = 0; I <100; I ++) {int x = random. next (image. width); int y = random. next (image. height); image. setpixel (X, Y, color. fromargb (random. next ();} // draw the border line G of the image. drawrectangle (new pen (color. silver), 0, 0, image. width-1, image. height-1); system. io. memorystream MS = new system. io. memorystream (); image. save (MS, system. drawing. imaging. imageformat. GIF); response. clearcontent (); response. contenttype = "image/GIF"; response. binarywrite (Ms. toarray ();} finally {G. dispose (); image. dispose ();}}}

 

ASPX page

<% @ Page Language = "  C #  " Autoeventwireup = "  True  " Codefile = "  Imagecode. aspx. CS  " Inherits = "  _ Default  " %> <! Doctype HTML public " -// W3C // dtd xhtml 1.0 transitional // en  "   "  Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd  " > <HTML xmlns = "  Http://www.w3.org/1999/xhtml  " > <Head runat = "  Server  " > <Title>  "  Form1 " Runat = "  Server  " > </Form> </body> 

Ii. Set the format mouse Style

 
Document. Body. style. cursor = "crosshair ";
 
Document. Body. style. cursor = "default ";

3. Document. Links get all links on the page

VaRLinks =Document. links;VaRArnames =[];For(VaRIInLinks) {arnames. Push (Links [I]);}For(VaRIInArnames) {document. Write (arnames [I]);}

Usage: Use the firebug plug-in of Firefox to run on the Sina homepage.

 

4. js onchange event, try throw catch for Form Verification

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml">  /*  (1) loss of focus (2) content has changed while meeting (1), (2) trigger onchange ------- throw:ProgramStream to catch (e) --------- carch (e): E can be any variable name  */  Function  Checkinfo (ID ){  Try  {  If (ID = "usrname" ){  If (Document. getelementbyid (ID). value = ""){ //  Throw, the program flow directly to catch               Throw "User name cannot be blank" ;}  If (Document. getelementbyid (ID). value. length> 20 ){  Throw "The length of the user name cannot exceed 20" ;}}  Else   If (ID = "psw" ){  If (Document. getelementbyid (ID). value = ""){  Throw "The password cannot be blank" ;}  If (Document. getelementbyid (ID). value. length> 20 ){  Throw "The password length cannot exceed 20" ;}  If  (Isnan (document. getelementbyid (ID). Value )){  Throw "The password must be a numerical value" ;}}}  Catch (E ){//  Arbitrary variable name  Alert (e );}} </SCRIPT>  User Name <Input type = "text" id = "usrname" onchange = "checkinfo (this. ID)"/> <br/> Password & Nbsp; Code <input type = "text" id = "psw" onchange = "checkinfo (this. ID) "/> </div> </body> 

5. js shields all images on the webpage;

VaRImages =Document. images;For(VaRIInImages) {images [I]. style. Visibility= "Hidden";}

Tutorial method: firebug, Which is used on the Sina homepage;

Effect: Images in IFRAME cannot be removed.

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.