How to Implement Remote Validation in ASP.Net MVC

Introduction

For some of our web applications we must validate the uniqueness of a field, for example when we create a user for the application then the user Id must be unique in order to provide login capability for the system. To do this we need to write code to make a remote server call to validate the field.

ASP.NET MVC provides a mechanism that can make a remote server call to validate the field without posting the entire form to the server. The "Remote" validation attribute is useful for that. This attribute is useful when we have a field that cannot be validated on the client side so we must validate it on to the server side.

Remote Attribute

The Remote attribute class uses the jQuery validation plug-in remote validator. With remote attribute, we need to pass a route name (to validate the text on the server side) or pass an action and controller name. This attribute enables a client-side validation and makes a call to the server when the actual validation is done.

For example, I want to create a customer registration form and it has a user id that must be unique within the database. I want to validate this user id using a remote attribute. The Following is the procedure to use a remote attribute in this case.


Step 1

Create a model for the customer and apply the report validation for the column user id.

  1. public class Customer   
  2. {  
  3.     public int CustomerId   
  4.     {  
  5.         get;  
  6.         set;  
  7.     }  
  8.     public string Name   
  9.     {  
  10.         get;  
  11.         set;  
  12.     }  
  13.     [RemoteAttribute("IsUserExists""Home")]  
  14.     public string UserId   
  15.     {  
  16.         get;  
  17.         set;  
  18.     }  
  19. }  
Step 2

Create a method in the controller to check the validation. This controller's action method must return a JsonResult object (true or false) depending on the validation performed.
  1. public class HomeController: Controller   
  2. {  
  3.     public JsonResult IsUserExists(string userId)   
  4.     {  
  5.         return IsExist(userId) ? Json(true, JsonRequestBehavior.AllowGet) : Json(false, JsonRequestBehavior.AllowGet);  
  6.     }  
  7.   
  8.     public bool IsExist(string userId)   
  9.     {  
  10.         // Write code to validate UserId weather is Userid already exists or not and base on return true and false.  
  11.         if (string.IsNullOrEmpty(userId)) return false;  
  12.         else if (userId == "jignesh"return false;  
  13.         else return true;  
  14.     }  
  15.   
  16. }  
Output


 
Pass Additional parameter

We can also send additional parameters from the screen by setting the additional field property. In the following example I want “name” properties along with a “UserId” property, so I have set an additional field property to “name".
  1. [RemoteAttribute("IsUserExists""Home",AdditionalFields="Name")]  
  2. public string UserId { getset; } 


We can get multiple additional properties by passing property names separated by commas into the AdditionalFields property of the remote attribute.
  1. [RemoteAttribute("IsUserExists""Home", AdditionalFields = "Name,Address”)]  
  2. public string UserId { getset; }  
Customize error message

We can also customize the error message by passing the named parameter “ErrorMessage”. By default the message is “{property Name} is invalid”. Using this named property we can change the error message text.
  1. [RemoteAttribute("IsUserExists""Home", ErrorMessage = "This UserId is already in use.")]  
  2. public string UserId { getset; }  
I hope this helps!