C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
ASP.NET MVC Input ValidationValidation of user input is necessary task for the application programmer. An application should allow only valid user input so that we get only desired information. ASP.NET MVC framework provides built-in annotations that we can apply on Model properties. It validates input and display appropriate message to the user. Commonly used Validation Annotations
Example Let's create an example that will validate input by using the annotations. To create the example, first we are creating a StudentsController and then a Student Model. Controller// StudentsController.csusing System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcApplicationDemo.Controllers { public class StudentsController : Controller { // GET: Students public ActionResult Index() { return View(); } } } Model// Student.csusing System.ComponentModel.DataAnnotations; namespace MvcApplicationDemo.Models { public class Student { public int ID { get; set; } // -- Validating Student Name [Required(ErrorMessage ="Name is required")] [MaxLength(12)] public string Name { get; set; } // -- Validating Email Address [Required(ErrorMessage ="Email is required")] [EmailAddress(ErrorMessage = "Invalid Email Address")] public string Email { get; set; } // -- Validating Contact Number [Required(ErrorMessage = "Contact is required")] [DataType(DataType.PhoneNumber)] [RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Not a valid Phone number")] public string Contact { get; set; } } } View// Index.cshtml@model MvcApplicationDemo.Models.Student @{ ViewBag.Title = "Index"; } <h2>Index</h2> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>Student</h4> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Contact, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Contact, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Contact, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Create" class="btn btn-default" /> </div> </div> </div> } <div> @Html.ActionLink("Back to List", "Index") </div> @section Scripts { @Scripts.Render("~/bundles/jqueryval") } Output: To see the output, right click on the Index.cshtml file and select view in browser. This will produce the following result. As we can see that it validates form fields and display error message to the user. In the below screenshot, we are validating that the entered data is as expected.
Next TopicASP.NET MVC Entity Framework
|