-
Remote Validation in ASP.NET Core MVC
-
In previous sections, we explored Model Validation, Data Annotations, and Custom Validation in ASP.NET Core MVC. These validation techniques ensure that user input is correct, but they are typically executed after the user submits the form.
Modern applications, however, aim to provide instant feedback to users while they are still filling out the form. Waiting until form submission can interrupt the user experience.
This is where Remote Validation becomes important.
Remote Validation allows validation to occur in real time by making a server call using AJAX, without submitting the entire form.
What is Remote Validation?
Remote Validation is a technique in ASP.NET Core MVC that validates a field by calling a server-side action method using AJAX.
It provides immediate feedback to the user while interacting with a form.
Remote Validation is a hybrid approach — it behaves like client-side validation but relies on server-side processing.
In simple terms, it feels like client-side validation because it provides instant feedback, but the actual validation logic runs on the server.
It is commonly used in scenarios such as:
- Checking if an email already exists
- Validating usernames
- Ensuring unique values in a database
Why Remote Validation is Important
- Provides instant feedback to users
- Improves user experience
- Reduces unnecessary form submissions
- Allows validation against database or server logic
How to Enable Client-Side Validation
Remote Validation depends on client-side validation. If client-side validation is not enabled, Remote Validation will not work.
In ASP.NET Core MVC, client-side validation is enabled using jQuery Validation and Unobtrusive Validation.
Step 1: Include Required Scripts
Make sure the following scripts are included in your layout or view:
<script src="~/lib/jquery/dist/jquery.min.js"></script> <script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script> <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
These scripts enable validation behavior in the browser.
Step 2: Use Validation Tag Helpers
Ensure your Razor view uses validation helpers:
<form method="post"> <input name="Email" /> <span> @Html.ValidationMessage("Email") </span> </form>
Step 3: Ensure Unobtrusive Validation is Enabled
In ASP.NET Core, client-side validation is enabled by default. However, you must ensure that unobtrusive validation is not disabled.
Check your configuration:
builder.Services.AddControllersWithViews();
No additional configuration is usually required unless explicitly disabled.
Step 4: Verify HTML Output
When validation is enabled, ASP.NET Core generates validation attributes in HTML:
<input data-val="true" data-val-remote="Error message" data-val-remote-url="/Account/IsEmailAvailable" />
These attributes are used by jQuery to trigger validation.
Common Issue
If client-side validation is not working:
- Scripts are missing
- Scripts are loaded in wrong order
- Validation helpers are not used
Key Point
Remote Validation will not work unless client-side validation is properly enabled and configured.
Required Scripts
Remote Validation depends on client-side libraries:
- jQuery
- jQuery Validation
- jQuery Unobtrusive Validation
Without these scripts, remote validation will not function.
<script src="jquery.js"> </script> <script src="jquery.validate.js"> </script> <script src="jquery.validate.unobtrusive.js"> </script>
How Remote Validation Works
- User enters a value in the input field
- The field loses focus (blur event)
- An AJAX request is sent to the server
- The server validates the value
- A response is returned (true or error message)
- The UI updates instantly with the validation result
This process combines the responsiveness of client-side validation with the accuracy of server-side validation.
What Actually Happens Behind the Scenes
When you apply the Remote attribute on a model property, ASP.NET Core generates HTML attributes that are understood by jQuery Unobtrusive Validation.
These attributes automatically trigger an AJAX request to the server when the input field changes or loses focus.
As a result, developers do not need to write manual JavaScript or AJAX code.
Understanding the Hybrid Nature
Remote Validation is often misunderstood as pure client-side validation because of its instant behavior.
However, unlike JavaScript-based validation, it does not perform validation entirely in the browser.
Instead, it sends data to the server using AJAX and waits for a response.
This makes it different from both traditional server-side validation and pure client-side validation.
Type Where it Runs Behavior Client-side Validation Browser Instant, no server call Server-side Validation Server Runs after form submission Remote Validation Client + Server Instant feedback using server logic This hybrid nature makes Remote Validation especially useful for scenarios that require real-time validation with server dependency.
Step 1: Apply Remote Attribute
using Microsoft.AspNetCore.Mvc; public class User { [Remote("IsEmailAvailable", "Account")] public string Email { get; set; } }
This tells MVC to call a server-side method to validate the Email field.
Step 2: Create Controller Action
public class AccountController : Controller { public JsonResult IsEmailAvailable(string Email) { bool exists = Email == "test@gmail.com"; if (exists) { return Json("Email already exists"); } return Json(true); } }
If validation fails, an error message is returned. Otherwise, true is returned.
Step 3: Razor View
<input name="Email" /> <span> @Html.ValidationMessage("Email") </span>
This connects validation messages to the UI.
UI Behavior (Real-Time Feedback)
Unlike server-side validation, Remote Validation provides feedback immediately.
Register
Email already existsImportant: This error appears when the user leaves the field (blur event), not when the submit button is clicked.
Real-World Scenario
Consider a registration form where each user must have a unique email address.
With Remote Validation, the system checks the email availability immediately when the user enters it, instead of waiting for form submission.
This improves usability and prevents unnecessary form submissions.
Advantages
- Instant validation feedback
- Better user experience
- Reduces server load from invalid submissions
- Supports database-driven validation
Common Mistakes in Remote Validation
Remote Validation is powerful, but it often fails due to small configuration issues. Understanding these common mistakes can save a lot of debugging time.
1. Missing Required Scripts
If jQuery, jQuery Validation, or jQuery Unobtrusive scripts are not included, Remote Validation will not work.
Result: No AJAX call is triggered.
2. Incorrect Action Method Signature
The parameter name in the controller must match the model property name.
public JsonResult IsEmailAvailable(string Email)
If names do not match, validation will fail silently.
3. Not Returning Proper Response
The action method must return either:
- true → validation passed
- error message → validation failed
return Json(true); return Json("Email already exists");
4. Not Using Correct Model Binding
The input field name must match the model property.
If mismatched, the value will not be sent correctly.
5. Forgetting to Enable Client-Side Validation
Remote Validation depends on client-side validation being enabled.
If disabled, Remote Validation will not trigger.
6. Testing Only on Submit
Remote Validation works on field change or blur event, not on submit.
Important: If you test only by clicking submit, you may think it is not working.
Key Takeaway
Most Remote Validation issues are configuration-related, not logic-related.
Ensuring proper setup of scripts, naming, and responses is essential for it to work correctly.
Summary
Remote Validation in ASP.NET Core MVC is a powerful technique that enables real-time validation by combining client-side interaction with server-side logic. It allows applications to validate user input instantly without requiring full form submission.
By using the Remote attribute along with AJAX-based requests, developers can build responsive and user-friendly forms.
Remote Validation complements traditional validation techniques and plays a key role in modern web applications.
Understanding Remote Validation helps you build faster, smarter, and more interactive user experiences.