Hello frnds ,in this article i will show you how to use client side validation in MVC 4,without any page refresh we can validate data .Before start it i wish to inform you that visual studio 2012 already has client side validation enabled in web.config page but you can easily enabled or disabled this code simply by writing this code
<configuration> <appSettings> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> </appSettings> </configuration>Now Add New Class in Model Folder and add the following code in itusing System.ComponentModel.DataAnnotations; namespace ClientSideValidation.Models { public class Test { [Required(ErrorMessage = "Name is Required")] public string Name { get; set; } [Required(ErrorMessage = "Email is Required")] [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" + @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" + @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", ErrorMessage = "Email is not valid")] public string Email { get; set; } }}Now Add Associate View which Should be strongly type view as shown in picture below
Thanks and keep enjoying...

0 comments:
Post a Comment