Validation
Validation
Unobtrusive Validation
This sample shows the basic usage of the unobtrusive validation.
Features
Create an account
Description
This sample shows the basic usage of the unobtrusive validation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | using MvcExplorer.Models; using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcExplorer.Controllers { public class ValidationController : Controller { [HttpGet] public ActionResult Register() { return View( new UserInfo() { Country = "" }); } [HttpPost] public ActionResult Register(UserInfo userInfo) { ViewBag.IsValid = ModelState.IsValid; return View(userInfo); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | @model UserInfo @if (ViewBag.IsValid == true ) { < p >< h3 > @Html .Raw(Resources.Validation.Register_RegistrationComplete)</ h3 ></ p > < div style = "height:200px" > @Html .Raw(Resources.Validation.Register_CongratulationsTo) @Model .Name @Html .Raw(Resources.Validation.Register_RegistrationSuccessfully) </ div > } else { @section Scripts{ @Scripts .Render( "~/jquery" ) @Scripts .Render( "~/jqueryval" ) } < h3 > @Html .Raw(Resources.Validation.Register_CreateAnAccount)</ h3 > < br /> using (Html.BeginForm()) { < div class = "container" style = "width: 100%" > < div class = "row" > < div class = "col-md-2" > @Html .LabelFor(m => m.Name) </ div > < div class = "col-md-2" > @Html .C1().InputMaskFor(m => m.Name).Width( "100%" ) </ div > < div class = "col-md-4" > @Html .ValidationMessageFor(m => m.Name) </ div > </ div > < div class = "row" > < div class = "col-md-2" > @Html .LabelFor(m => m.Email) </ div > < div class = "col-md-2" > @Html .C1().InputMaskFor(m => m.Email).Width( "100%" ) </ div > < div class = "col-md-4" > @Html .ValidationMessageFor(m => m.Email) </ div > </ div > < div class = "row" > < div class = "col-md-2" > @Html .LabelFor(m => m.Password) </ div > < div class = "col-md-2" > @Html .PasswordFor(m => m.Password, new { @class = "form-control" }) </ div > < div class = "col-md-4" > @Html .ValidationMessageFor(m => m.Password) </ div > </ div > < div class = "row" > < div class = "col-md-2" > @Html .LabelFor(m => m.ConfirmPassword) </ div > < div class = "col-md-2" > @Html .PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" }) </ div > < div class = "col-md-4" > @Html .ValidationMessageFor(m => m.ConfirmPassword) </ div > </ div > < div class = "row" > < div class = "col-md-2" > @Html .LabelFor(m => m.Phone) </ div > < div class = "col-md-2" > @Html .C1().InputMaskFor(m => m.Phone).Mask( "+8600000000000" ).Width( "100%" ) </ div > < div class = "col-md-4" > @Html .ValidationMessageFor(m => m.Phone) </ div > </ div > < div class = "row" > < div class = "col-md-2" > @Html .LabelFor(m => m.Country) </ div > < div class = "col-md-2" > @Html .C1().AutoCompleteFor(m => m.Country).Bind(b => b.Bind(UsersData.Countries)) </ div > < div class = "col-md-4" > @Html .ValidationMessageFor(m => m.Country) </ div > </ div > < div class = "row" > < div class = "col-md-2" > @Html .LabelFor(m => m.Industry) </ div > < div class = "col-md-2" > @Html .C1().ComboBoxFor(m => m.Industry).Bind(b => b.Bind(UsersData.Industries)).SelectedValuePath( "value" ).DisplayMemberPath( "name" ) </ div > < div class = "col-md-4" > @Html .ValidationMessageFor(m => m.Industry) </ div > </ div > < div class = "row" > < div class = "col-md-2" > @Html .LabelFor(m => m.Birthdate) </ div > < div class = "col-md-2" > @Html .C1().InputDateFor(m => m.Birthdate) </ div > < div class = "col-md-4" > @Html .ValidationMessageFor(m => m.Birthdate) </ div > </ div > < div class = "row" > < div class = "col-md-2" > @Html .LabelFor(m => m.FavoriteColor) </ div > < div class = "col-md-2" > @Html .C1().InputColorFor(m => m.FavoriteColor) </ div > < div class = "col-md-4" > @Html .ValidationMessageFor(m => m.FavoriteColor) </ div > </ div > < div class = "row" > < div class = "col-md-2" > @Html .LabelFor(m => m.Skills) </ div > < div class = "col-md-2" > @Html .C1().MultiSelectFor(m => m.Skills).Bind(b => b.Bind(UsersData.SkillsList)) </ div > < div class = "col-md-4" > @Html .ValidationMessageFor(m => m.Skills) </ div > </ div > < div class = "row" > < div class = "col-md-2" > @Html .LabelFor(m => m.Hobbies) </ div > < div class = "col-md-2" > @Html .C1().MultiAutoCompleteFor(m => m.Hobbies).Bind(b => b.Bind(UsersData.HobbyList)) < div class = "text-muted" > @Html .Raw(Resources.Validation.Register_HobbiesPrompt)</ div > </ div > < div class = "col-md-4" > @Html .ValidationMessageFor(m => m.Hobbies) </ div > </ div > < p >< input type = "submit" value = "@(Resources.Validation.Register_Register)" class = "btn btn-primary" /></ p > </ div > } } @section Description{ @Html .Raw(Resources.Validation.Register_Text0) } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | using System; using System.Linq; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Drawing; namespace MvcExplorer.Models { public class UserInfo { public UserInfo() { Birthdate = DateTime.Now; } public int Id { get ; set ; } [Required] [RegularExpression(pattern: "^[a-zA-Z0-9]{4,10}$" , ErrorMessageResourceType = typeof (Resources.Validation), ErrorMessageResourceName = "Register_Name_ErrorMessage" )] public string Name { get ; set ; } [Required] [RegularExpression(pattern: @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$" , ErrorMessageResourceType = typeof (Resources.Validation), ErrorMessageResourceName = "Register_Email_ErrorMessage" )] public string Email { get ; set ; } [Required] [StringLength(16, MinimumLength = 6)] public string Password { get ; set ; } [Required] [Compare( "Password" )] public string ConfirmPassword { get ; set ; } [Required] [StringLength(11, MinimumLength = 8)] public string Phone { get ; set ; } [Required] public string Country { get ; set ; } [Required] public string Industry { get ; set ; } [Required] public DateTime Birthdate { get ; set ; } [Required] public Color? FavoriteColor { get ; set ; } [Required] public string [] Skills { get ; set ; } [Required] public string [] Hobbies { get ; set ; } } } |