The action in a Controll is responsible for managing the view based on the verification code in the MVC2 architecture.
This is the process of creating a verification code image in controll.
Public fileresult confirmcode () <br/>{< br/> // generates a four-digit random number, converts it to the string type, and passes in the session, the four digits are extracted from 1000-9999 by the number of </P> <p> machines. </P> <p> string confirmcode = string. empty; <br/> random Rand = new random (); <br/> confirmcode = Rand. next (1000,999 9 ). tostring (); <br/> session ["quickchannelconfirmcode"] = confirmcode;
// Any color in nature can be composed of three colors: Red, green, and blue.
Can be quantified as (0-255). Therefore, you only need to control the values of red, green, and blue.
The color of the verification code can be randomly generated.
// So I first instantiate two random number objects rand1 and rand2, and then
In the number, the next method extracts numbers from (0-255 ).
// In order to make the random colors clear, we control the blue color in RGB, because the blue color (145-255)
The color is bright.
// Two logical expressions are used here. The value Logic of blue1 is assumed (the value of red + the value of green is large ).
At 400, the value of blue1 is 0; otherwise, the value of blue1 is (400-red-green ))
// Because the above logic may cause the value of blue1 to be greater than 255, in order to prevent outgoing
Now, set another logical expression. If the value is greater than 255, the value of blue is 255.
// Use the fromargb method of the color class to generate a random color.
Random rand1 = new random (); <br/> random rand2 = new random (); <br/> int Red = rand1.next (256 ); <br/> int Green = rand2.next (256); <br/> int blue1 = (Red + green> 400 )? 0: 400-red-green; <br/> int blue2 = (blue1> 255 )? 255: blue1; <br/> color Col = color. fromargb (red, green, blue2 );
After the numbers and colors are generated, we need to combine these elements and draw them into an image. From the plotting Point of View
For example, a picture must be generated with a painting area and a frame that wraps it.
So we first instantiate the bitmap class to generate a painting area BMP with a width of imagewidth and a length of 20.
Encapsulate it into frame g, so that a painting area (BMP) exists in G ).
G. Clear (color. White) clears all colors in the painting area and fills them in white.
Similar to the starting state of PS. In this case, we draw a rectangle for G.
V. The tool, start coordinate, and end coordinate for drawing the panel.
(0, 0) Draw the coordinates to (imagewidth-1, 19.
After the rectangle is drawn, the content in the rectangle is drawn. Because the string type is drawn, four parameters need to be prepared.
Number 1. The drawn content (the number generated above), 2. Font, 3. What "tool" is used to draw
4. Where to start painting (start position). Based on the above four points, we can use the drawstring method to plot the verification
Verification code.
Int imagewidth = confirmcode. length * 13; <br/> bitmap BMP = new Bitmap (imagewidth, 20); <br/> graphics G = graphics. fromimage (BMP); <br/> G. clear (color. white); <br/> G. drawrectangle (new pen (color. lightgray, 1), imageWidth-1, 19); <br/> Font font = new font ("Arial", 16 ); <br/> brush = new solidbrush (COL); // col is the color of the above random <br/> point po = new point (0, 0 ); <br/> G. drawstring (confirmcode, Font, brush, po );
// Output the generated image. For the output, first we need to tell the reader the type of the image output (from
To avoid garbled characters, specify the output type as image/JPEG.
// The image is output as a stream to the browser. Therefore, you must first save the image BMP into the memory stream ms and format it.
The image type is JPEG.
// Set the starting position of the memory stream ms to 0.
// Return to the view as a file stream.
Response. contenttype = "image/JPEG"; <br/> memorystream MS = new memorystream (); <br/> BMP. save (MS, imageformat. JPEG); <br/> MS. seek (0, seekorigin. begin); <br/> return New filestreamresult (MS, "image/JPEG"); <br/>}
After the logic in Controll is created, we return to the view and create a simple page (without CSS binding), and
Declare to call jquery.
<Form ID = "form1" method = "Post"> <br/> <label> Name: </label> <br/> <input type = "text" name = "nametextbox" id = "nametextbox"/> <br/> <label> Verification Code: </label> <br/> <input type = "text" name = "textbox1" id = "textbox1"/> <br/> <label> email </label> <br/> <input type = "text" name = "email" id = "email"/> <br/> <br/> <span> <a href =" javascript: refreshpic () "mce_href =" javascript: refreshpic () "> cannot see clearly, change one </a> </span> <br/> <label> <a href = ".. /.. /content/ohoopee2.jpg "mce_href =" content/ohoopee2.jpg "rel =" bigpic "> (click to enlarge) </a> </label> <br/> <a href = "javascript: Check ()" mce_href = "javascript: Check () "> confirm </a> <br/> </form>
On this page, we need to consider several issues. First, what should I do when two textbox values are empty?
In general, when this happens, you can use js to determine at the page level.
So we wrote a validateaddress () function to judge when the value of textbox is null.
In addition, we also need to consider regular expressions to prevent the user from losing characters in disorder.
Function validateaddress () {<br/> if ($ ("# nametextbox "). val () = "") {<br/> alert ("Enter name"); <br/> return; <br/>}< br/> if ($ ("# textbox1 "). val () = "") {<br/> alert ("Enter the Verification Code"); <br/> return; <br/>}< br/> if ($ ("# email "). val () = "") {<br/> alert ("Enter the email address"); <br/> return; <br/>}< br/> if ($ ("# email "). val ()! = "") {<Br/> var Reg =/^ [a-z0-9] ([a-z0-9] * [-_/.]? [A-z0-9] +) * @ ([a-z0-9] * [-_]? [A-z0-9] +) + [/.] [A-Z] {2, 3} ([/.] [A-Z] {2 })? $/I; <br/> If (! Reg. test ($ ("# email "). val () {<br/> alert ("enter the correct email address"); <br/> return; <br/>}</P> <p> $ ("# form1 "). submit (); </P> <p>}
The second problem that needs to be considered is the replacement of the Verification Code when it is not clear. Refer to the "not clear" link of the mailbox, we
Similarly, a javascript code is used to bind it to the text.
The function adopts the partial refresh method and uses the document method to locate the src attribute of the IMG control.
When you click, execute the server-side action ("/home/confirmcode") to make the page
Partial refresh when refreshing.
Function refreshpic () {<br/> document. getelementbyid ("confirmimage"). src = "/home/confirmcode"; <br/>}
In addition, you can determine whether the verification code is correct. We use the URL-based value passing method to determine on the server side.
The function uses the getscript method in jquery to pass the value to the server through the URL,
The server performs logical judgment and returns the result to the page through the server. js returns the result based on the returned result.
Prompt text.
PAGE method:
Function check () {<br/> $. getscript ("/home/checkcode? Code = "+ $ (" # textbox1 "). val (), function (j) {<br/> If (eval (j) = true) {<br/> validateaddress (); <br/> alert ("verified"); <br/>}< br/> else {</P> <p> refreshpic (); </P> <p> alert ("Verification code error! Please check "); <br/>}< br/>}) <br/>}
Server logic:
Note that when the session is null and "", the system may report an error if you do not pay attention to it.
In addition, you must set the resource release after the session is used. Otherwise, the resource may be released.
The shortage affects the efficiency of website operations.
Public actionresult checkcode () <br/>{< br/> string quickchannalconfirmcode = request. querystring ["code"]. tostring (); </P> <p> If (session ["quickchannelconfirmcode"] = NULL | "". equals (session ["quickchannelconfirmcode"]. tostring () <br/>{< br/> return content ("false "); <br/>}</P> <p> string yanzheng = session ["quickchannelconfirmcode"]. tostring (); <br/> session. remove ("quickchannelconfirmcode"); <br/> If (quickchannalconfirmcode = yanzheng) <br/>{</P> <p> return content ("true "); </P> <p >}< br/> else <br/>{< br/> return content ("false"); <br/>}< br/>}
Therefore, a simple verification code function can be achieved ~~~~