Look at given the following points:

1. Open Visual Studio and seelct "File" -> "New" -> "Project...".

empty web application
Image 1

internet application
Image 2

2. After creating the application, go to the web.config file. Here you can see two keys already added to enable validation.

web config
Image 3

3. Now go to Model -> Add New Item -> Add New Class with the name Employee and do the following coding. Here we need to add using System.ComponentModel.DataAnnotations.

Namespace

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.ComponentModel.DataAnnotations;
  6. namespace ClientSideValidationInMVC.Models
  7. {
  8. public class Employee
  9. {
  10. [Required (ErrorMessage="Name is Required")]
  11. public string Name { get; set; }
  12. [Required(ErrorMessage = "Email is Required")]
  13. [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
  14. @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
  15. @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$",
  16. ErrorMessage = "Email is not valid")]
  17. public string Email { get; set; }
  18. [Required(ErrorMessage = "Password is Required")]
  19. public string Password { get; set; }
  20. }
  21. }
4. Now I will add a new Controller name as EmployeeRegFormController.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using ClientSideValidationInMVC.Models;
  7. namespace ClientSideValidationInMVC.Controllers
  8. {
  9. public class EmployeeRegFormController : Controller
  10. {
  11. //
  12. // GET: /EmployeeRegForm/
  13. public ActionResult Index()
  14. {
  15. return View();
  16. }
  17. [AcceptVerbs(HttpVerbs.Post)]
  18. public ActionResult Index(Employee model)
  19. {
  20. if (ModelState.IsValid)
  21. {
  22. ViewBag.Name = model.Name;
  23. ViewBag.Email = model.Email;
  24. ViewBag.Password = model.Password;
  25. }
  26. return View(model);
  27. }
  28. }
  29. }
5. Now right-click on Index and Add a View.

add view
Image 4

6. Open the view (Index.cshtml).
  1. @model ClientSideValidationInMVC.Models.Employee
  2. @{
  3. ViewBag.Title = "Employee Registration Form";
  4. }
  5. @if (ViewData.ModelState.IsValid)
  6. {
  7. if (@ViewBag.Name != null)
  8. {
  9. <b><span style="font-family: Verdana; font-size: 10pt;">Name : @ViewBag.Name<br />
  10. Email : @ViewBag.Email<br />
  11. </span>
  12. </b>
  13. }
  14. }
  15. <br />
  16. <br />
  17. @using (Html.BeginForm())
  18. {
  19. @Html.ValidationSummary(true)
  20. <fieldset>
  21. <legend>Employee</legend>
  22. <div class="editor-label">
  23. @Html.LabelFor(model => model.Name)
  24. </div>
  25. <div class="editor-field">
  26. @Html.EditorFor(model => model.Name)
  27. @Html.ValidationMessageFor(model => model.Name)
  28. </div>
  29. <div class="editor-label">
  30. @Html.LabelFor(model => model.Email)
  31. </div>
  32. <div class="editor-field">
  33. @Html.EditorFor(model => model.Email)
  34. @Html.ValidationMessageFor(model => model.Email)
  35. </div>
  36. <div class="editor-label">
  37. @Html.LabelFor(model => model.Password)
  38. </div>
  39. <div class="editor-field">
  40. @Html.PasswordFor(model => model.Password)
  41. @Html.ValidationMessageFor(model => model.Password)
  42. </div>
  43. <p>
  44. <input type="submit" value="Register" />
  45. </p>
  46. </fieldset>
  47. }
  48. @section Scripts {
  49. @Scripts.Render("~/bundles/jqueryval")
  50. }
7. Now run your application:

emp registration
Image 5

8. Type Name, Email and Password.

email not valid
Image 6

output
Image 7