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.