ASP. NET MVC Website Development Summary (v)--ajax Verification code for asynchronous submission Form

Source: Internet
Author: User

First of all, a question: in the development of the site, the use of verification code to prevent malicious submission of the form, then how to implement when the verification code error, just refresh the verification code, and other filling information does not change?

First of all, why this need: to submit the registration information page, for example, the general registration requires the user to fill in a Verification code information (to prevent malicious machine registration), and this code will be submitted to the background to do the comparison, if the wrong will not check the other submission information and directly return to the browser side prompt Captcha error. If you simply submit the data directly to the specified URLusing the form form, the entire page will inevitably be refreshed once the error message is returned to the browser, which is not what the user wants, Because some of the other correct information needs to be re-filled, which makes the user experience not very good. And this problem can be implemented by using Ajax to submit forms asynchronously. (This is just one solution)

Here's a look at the specific implementation:

Foreground Html code:

<!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 strict//en" "Http:// "><HTMLxmlns= "">    <Head>        <title>Verification Code of AJAX asynchronous Submission form</title>    </Head>    <Body>        <formAction= "Javascript:ajaxpostdata ()"Method= "POST">        <label>User name:</label>        <inputname= "Account"class= "Form-control"ID= "Account"type= "text"placeholder= "User name"Required= "Required" />        <label>Password:</label>        <inputname= "Password"class= "Form-control"ID= "Password"type= "Password"placeholder= "Password"Required= "Required" />        <label>Verification Code:</label>        <imgID= "Valicode"class= "Validcode"src= "/home/getvalidatecode"alt= "Verification Code"title= "Click to refresh" />        <inputname= "Code"class= "Form-control"ID= "Code"type= "text"placeholder= "Verification Code"Required= "Required" />        <Buttontype= "Submit">Submit</Button>    </form>        <Scriptsrc= "~/scripts/jquery-1.12.1.min.js"></Script>    <Script>        //Refresh Verification Code        functionRefreshvalicode () {document.getElementById ("Valicode"). SRC= "/getvalidatecode?time=" + (NewDate ()). GetTime (); }            functionAjaxpostdata () {varCode=document.getElementById ("Code"). Value; var Account=document.getElementById (" Account"). Value; varPassword=document.getElementById ("Password"). Value; $.ajax ({URL:'/user/register',//destination URL to which the data is submittedType:'Post',//Post mode submissionAsync:true,//Asynchronous Commitdata: {account:account, Password:password, Code:code},//submitted DataSuccess:function(data) {//send a successful callback function                if(data.success) {alert ("Registration failed!"); }                Else{alert ("Registration Successful!"); Refreshvalicode ();//Refresh Verification Codedocument.getElementById ("Code"). Value= "";//Empty input Box}}, Error:function() {alert ("request failed! Please resubmit!");        }            }); }    </Script>    </Body></HTML>
View Code

Note: jquery-1.12.1.min.js need to download references by themselves.

Background C # code:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;usingSystem.Web.Security;namespacetest.controllers{ Public classUsercontroller:controller {/// <summary>        ///Register/// </summary>        /// <param name= "code" >Verification Code</param>        /// <returns></returns>[HttpPost] PublicActionResult Register (stringNamestringPasswordstringcode) {            //session["Registercode"] set its value when generating a verification code            if(string. Isnullorwhitespace (code) | | session["Registercode"]. ToString (). ToUpper ()! =code. ToUpper ()) {returnJson (New{success =false}); }            Else            {                //Other operations ...                returnJson (New{success =true}); }                    }    }}
View Code

This knowledge sharing is here, please look forward to the next share. ^_^

< my blog homepage;:

<wing Studio homepage;:

ASP. NET MVC Website Development Summary (v)--ajax Verification code for asynchronous submission Form

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: 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.