11+ Year IT Industry Experience, Working as Technical Lead with Capgemini | Consultant | Leadership and Corporate Trainer | Motivational and Technical Speaker | Career Coach | Author | MVP | Founder Of RVS Group | Trained more than 4000+ IT professionals | Azure | DevOps | ASP.NET | C# | MVC | WEB API | ANGULAR | TYPESCRIPT | MEAN | SQL | SSRS | WEB SERVICE | WCF... https://bikeshsrivastava.blogspot.in/ http://bikeshsrivastava.com/

Remote validation in MVC 5,To Check UserName and MailId is Already exist or not.

Introduction  
In this article I am going to explain about "Remote Validation"  using MVC 5.Remote validation is mainly used to  check user or mailid exist or not in database ,also you can use for any type of validation using Remote Attribute.
 Most of web application needs to check user or mailid exist or not in database ,in that developers needs to write huge amount of code,that's why i am going to achieve this task in very simple steps .Remote validation is normally works as jquery validation.
Before explain about "Remote Validation" ,if you are beginner in MVC that's why i am listing below some important links. 
What is Remote validation 
 Remote validation permits the developer to call the controller action using client side script. This is to a great degree valuable when you need to perform out a back end query without performing a full server side postback.
Implement of  "Remote Validation" in MVC Application.
Remote validation is only an Ajax call which approves the client's information. This can undoubtedly be proficient without using ASP.NET MVC 5. The situation for our demo is very basic. We have to ensure that the EmpNameof the new user is one of a kind. The execution beneath makes a basic structure which contains the TextBox control with the id EmpName" and other TextBox control with Id EmpMail. 
Follow some simple steps to use remote validation in MVC 5
Step 1:- Create a MVC application.see image...

 Step 2:-Create a model "Employee.cs" inside "Model" folder.use this code.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.   
  7. namespace RemoteValidation.Models  
  8. {  
  9.     public class Employee  
  10.     {  
  11.         //To Check userName  
  12.         [Remote("IsEmpNameExist""Validation", ErrorMessage = "Employee name already exist")]  
  13.         public string EmpName  
  14.         {  
  15.             get;  
  16.             set;  
  17.         }  
  18.         //check User name and mailId.  
  19.         [Remote("IsEmpNameandMailExist""Validation", ErrorMessage = "EmialId is already exist", AdditionalFields = "EmpName")]  
  20.         public string EmpMail  
  21.         {  
  22.             get;  
  23.             set;  
  24.         }  
  25.     }  
  26.     //Set static value in model.  
  27.     public static class EmployeeStaticData  
  28.     {  
  29.         public static List<Employee> UserList  
  30.         {  
  31.             get  
  32.             {  
  33.                 return new List<Employee>  
  34.                 {  
  35.                     new Employee  
  36.                     {  
  37.                         EmpName = "Bikesh", EmpMail = "Bikesh.srivastava@hytechpro.com"  
  38.                     },  
  39.                     new Employee  
  40.                     {  
  41.                         EmpName = "Navdeep", EmpMail = "Navdeep.kumar@hytechpro.com"  
  42.                     }  
  43.   
  44.                 };  
  45.             }  
  46.         }  
  47.   
  48.     }  
  49. }  
 Step 3-Create a controller "ValidationController.cs" to Implement validation.In this controller i am creating action which is used to check user exist or not ,also you can check any type validation .use this code.  
  1. using RemoteValidation.Models;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.Mvc;  
  7.   
  8. namespace RemoteValidation.Controllers  
  9. {  
  10.     public class ValidationController : Controller  
  11.     {  
  12.         //For check both at a time .mailid and empName.  
  13.         [HttpGet]  
  14.         public JsonResult IsEmpNameandMailExist(string Empname, string EmpMail)  
  15.         {  
  16.              
  17.             bool isExist = EmployeeStaticData.UserList.Where(u=>u.EmpName.ToLowerInvariant().Equals(Empname.ToLower()) && u.EmpMail.ToLowerInvariant().Equals(EmpMail.ToLower())).FirstOrDefault() != null;  
  18.             return Json(!isExist, JsonRequestBehavior.AllowGet);  
  19.         }  
  20.         //To check only EmpName  
  21.         [HttpGet]  
  22.         public JsonResult IsEmpNameExist(string Empname)  
  23.         {  
  24.   
  25.             bool isExist = EmployeeStaticData.UserList.Where(u => u.EmpName.ToLowerInvariant().Equals(Empname.ToLower()))!= null;  
  26.             return Json(!isExist, JsonRequestBehavior.AllowGet);  
  27.         }  
  28.     }  
  29. }  
Step 4-Create controller to execute your action ,In this controller you can create any type of action to perform your task.use this code .

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using RemoteValidation.Models;  
  7.   
  8. namespace RemoteValidation.Controllers  
  9. {  
  10.     public class EmployeeController : Controller  
  11.     {  
  12.        //my action   
  13.             [HttpGet]  
  14.             public ActionResult AddUser()  
  15.             {  
  16.                 Employee model = new Employee();  
  17.                 return View(model);  
  18.             }  
  19.           
  20.     }  
  21. }  
 Step 5-Right click on Action name "AddUser"  add view. In this view i am binding model "Employee" .using  Razor control i am creating a login page which is designed by bootstrap and css,jquery. use this code.
  
  1. @model RemoteValidation.Models.Employee  
  2.   
  3. <div class="panel panel-primary">  
  4.     <div class="panel-heading panel-head"> Add User</div>  
  5.     <div class="panel-body">  
  6.         @using (Html.BeginForm())  
  7.         {  
  8.             <div class="form-horizontal">  
  9.                 <div class="form-group">  
  10.                     @Html.LabelFor(model => model.EmpName, new { @class = "col-lg-2 control-label" })  
  11.                     <div class="col-lg-9">  
  12.                         @Html.TextBoxFor(model => model.EmpName, new { @class = "form-control" })  
  13.                         @Html.ValidationMessageFor(model => model.EmpName)  
  14.                     </div> </div>  
  15.                     <div class="form-group">  
  16.                         @Html.LabelFor(model => model.EmpMail, new { @class = "col-lg-2 control-label" })  
  17.                         <div class="col-lg-9">  
  18.                             @Html.TextBoxFor(model => model.EmpMail, new { @class = "form-control" })  
  19.                             @Html.ValidationMessageFor(model => model.EmpMail)  
  20.                         </div>  
  21.                     </div>  
  22.                     <div class="form-group">  
  23.                         <div class="col-lg-9"></div> <div class="col-lg-3">  
  24.                             <button class="btn btn-success"  
  25.                                     id="btnSubmit" type="submit">  
  26.                                 Submit  
  27.                             </button>  
  28.                         </div>  
  29.                     </div>  
  30.                 </div>               
  31.               
  32.   
  33.   
  34.   
  35.         }  
  36.     </div>  
  37. </div>  
  38.             @section scripts  
  39.   
  40.             {   
  41.                 @Scripts.Render("~/bundles/jqueryval")  
  42.             }  
 Step 6:-Don't forget this step.add these lines in "Web.config" file.use this code inside "<appseting>".
  1. <appSettings>     
  2.     <add key="ClientValidationEnabled" value="true" />  
  3.     <add key="UnobtrusiveJavaScriptEnabled" value="true" />  
  4.   </appSettings>  
 Step 7:-This step is also important because without adding "jquery.validate" and "jquery.validate.unobtrusive"  because this library working on background.needs to add in "bundle.config" file.use this code.
  1. using System.Web;  
  2. using System.Web.Optimization;  
  3.   
  4. namespace RemoteValidation  
  5. {  
  6.     public class BundleConfig  
  7.     {  
  8.         // For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862  
  9.         public static void RegisterBundles(BundleCollection bundles)  
  10.         {  
  11.             bundles.Add(new StyleBundle("~/Content/css").Include(  
  12.                 "~/Content/css/bootstrap.css",  
  13.                 "~/Content/css/font-awesome.css",  
  14.                 "~/Content/css/site.css"));  
  15.   
  16.             bundles.Add(new ScriptBundle("~/bundles/jquery").Include(  
  17.                 "~/Scripts/jquery-{version}.js"));  
  18.   
  19.             bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(  
  20.                 "~/Scripts/jquery.validate*"));  
  21.         }  
  22.     }  
  23. }  
 Step 8:-Now set your "route.config" default controller and action according your application.and run.If you have configured all setting and implemented  code properly and followed all steps. you can see output screen like this...

 According this image you can see ,i am typing "Bikesh" ,showing error message "Employee name already exists" because i have written hard code "Bikesh" in model.
There are two situation to check validation.
1.you can check only Employee Name
2.you can check both condition "Employee Name" and Email Id.
I hope you understand all things about "Remote validation".if any problem you can download project and run on your system. 
You have just read an article that categorized by title MVC by title Remote validation in MVC 5,To Check UserName and MailId is Already exist or not.. You can bookmark this page with a URL https://bikeshsrivastava.blogspot.com/2016/09/part-37remote-validation-in-mvc-5to.html. Thank You!
Author: Bikesh Srivastava - Wednesday, September 28, 2016

1 comments for "Remote validation in MVC 5,To Check UserName and MailId is Already exist or not."

Life Is Complicated, But Now programmer Can Keep It Simple.