Hi Friends ,
I Have created one example Project , I want to put Validation for that Page.How to do and whate are the ways to do that task,If Provide Me example
Thx In Advance
MVC have very easy Client side and server side validation
If suppose Javascript is turned off by the client , then server side validation is there which will validate the data.. With out server side validation , the data will not be processed..
Server side validation :
public ActionResult Edit(Object ObjectName) { if (ModelState.IsValid) // This is what server side validation { try { Your code goes here } catch (Exception ex) { Exception handling } } // if you reach here, this means your validation failed return View(ViewName , Object); }
Client Side Validation :
@using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <hr /> @Html.ValidationSummary(true) <div class="form-group"> @Html.Label(Messages.UserName, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.UserName) @Html.ValidationMessageFor(model => model.UserName) // Client Side validation </div> </div> <div class="form-group"> @Html.Label(Messages.Password, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Password) @Html.ValidationMessageFor(model => model.Password) // Client Side validation </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="@Messages.Login" class="btn btn-default" id="btnlogin"/> </div> </div> </div> }
// Below Scripts are needed for Client side validation
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
public partial class WorkedHourEntry { public int ID { get; set; } [Display(Name = "UserName", ResourceType = typeof(Resources.Resources))] public string UserName { get; set; } [Required(ErrorMessage = "*")] // We have to mention here the field is required or not [Display(Name = "BugId", ResourceType = typeof(Resources.Resources))] public Nullable<int> BugId { get; set; } [Required(ErrorMessage = "*")] [DataType(DataType.Date)] [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] [Display(Name = "WorkedDate", ResourceType = typeof(Resources.Resources))] public Nullable<System.DateTime> WorkedDate { get; set; } [Required(ErrorMessage = "*")] [Display(Name = "WorkedHours", ResourceType = typeof(Resources.Resources))] public Nullable<decimal> WorkedHours { get; set; } [Required(ErrorMessage = "*")] [Display(Name = "Activity", ResourceType = typeof(Resources.Resources))] public string Activity { get; set; } [DataType(DataType.MultilineText)] [Display(Name = "Notes", ResourceType = typeof(Resources.Resources))] [StringLength(100, ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "CharactersTooLong")] // Specify the string length public string Notes { get; set; } public virtual ActivityMaster ActivityMaster { get; set; } public virtual TaskMaster TaskMaster { get; set; } }
Mark as answer if appropriate