MVC Learning Series One----validation series client-side validation

Source: Internet
Author: User

Before we learned, server-side validation, in this article, we went on to learn client authentication, client authentication, using jquery and jquery plugins to implement "Jquery.validate.min.js and Jquery.validate.unobtrusive.min.js) "

In the service-side verification, the page must be submitted to the server for verification, if the data validation does not pass, the server will send a response to the client, and then the client according to the corresponding information, processing, and client authentication is different, the user input data, as long as a commit, the client will be validated first, If you do not pass the error, will not be submitted to the server for verification, if passed, the request will be transmitted to the server side.

If you are using more than VS2012, then the client-side authentication is enabled: "Of course you can also manually add the following configuration." 】

<configuration>
<appSettings>
<add key= "clientvalidationenabled" value= "true"/>
<add key= "unobtrusivejavascriptenabled" value= "true"/>
</appSettings>
</configuration>

We are still the previous project:

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel.DataAnnotations;usingSystem.Linq;usingsystem.web;namespaceServer_side_validation_in_mvc. models{ Public classStudentserver {[Required (errormessage="name is required")]         Public stringName {Get;Set; } [Required (ErrorMessage="e-Mail must")] [EmailAddress (ErrorMessage="e-mail format is incorrect")]         Public stringEmail {Get;Set; } }}
   Public actionresult Seversideindex ()        {            return  View ();        }        [HttpPost]          Public ActionResult Seversideindex (studentserver model)        {            if  (modelstate.isvalid)            {                = model. Name;                 = model. Email;            }             return View ();        }

The difference is, here, I add views, not the same:

Note that it is important to check the reference script library, which is to introduce jquery and jquery plugins, for client-side validation:

Modify the default route:

  Public Static voidregisterroutes (routecollection routes) {routes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); Routes. MapRoute (Name:"Default", URL:"{Controller}/{action}/{id}", defaults:New{controller ="Student", action ="Seversideindex", id =urlparameter.optional}); }

To run the project:

We add a breakpoint to the controller's post method

Direct Click button: No post method is called, and the validity of the data is verified directly on the client.

Enter the valid data:

The breakpoint is triggered, that is, after the client's validation is passed, it is submitted to the server for further processing.

Well, this is the client side validation, which is relatively straightforward. But be careful to introduce jquery plugins

MVC Learning Series One----validation series client-side validation

Related Article

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.