asp.net MVC5 Verification Series of client verification _ practical skills

Source: Internet
Author: User

In the previous study, server-side validation, this article, we went on to learn client authentication, client authentication, using jquery and jquery plug-ins to implement "Jquery.validate.min.js and Jquery.validate.unobtrusive.min.js) "

In server-side validation, 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 based on the appropriate information processing, and client-side validation is different, the user input data, as long as a submission, the client will be first validated, If you do not pass the error, will not be submitted to the server for verification, if passed, will upload the request to the server side.

If you are using more than VS2012 version, then the client-side authentication will be brought in: "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:

Using System;
Using System.Collections.Generic;
Using System.ComponentModel.DataAnnotations;
Using System.Linq;
Using System.Web;

Namespace Server_side_validation_in_mvc. Models
{public
  class Studentserver
  {
    [Required (errormessage= ' Name is required ')] public
    string Name { Get Set

    [Required (errormessage= "e-mail must be")]
    [EmailAddress (errormessage= "e-mail format is not")] public
    string Email { Get Set }
  }
}

Public ActionResult Seversideindex ()
    {return
      View ();
    }

    [HttpPost]
    Public ActionResult Seversideindex (Studentserver model)
    {
      if (modelstate.isvalid)
      {
        Viewbag.name = model. Name;
        Viewbag.email = 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 plug-ins for client verification:
Modify the default route:

 public static void RegisterRoutes (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: The Post method is not invoked to validate the legality of the data directly on the client.

Enter the legitimate data:

A breakpoint is triggered, that is, after client-side validation passes, it is not committed to the server for further processing.

OK, this is client-side validation, simpler. But be aware of introducing jquery Plug-ins

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.