How to check if user already exists on client-side in ASP.NET MVC 5?
You could use RemoteAttribute to perform client side validation with a server callback.
1) Add the following method to the AccountController
:
[AllowAnonymous]
public async Task<JsonResult> UserAlreadyExistsAsync(string email)
{
var result =
await userManager.FindByNameAsync(email) ??
await userManager.FindByEmailAsync(email);
return Json(result == null, JsonRequestBehavior.AllowGet);
}
2) Add Remote
attribute to Email
property of RegisterViewModel
class:
[Remote("UserAlreadyExistsAsync", "Account", ErrorMessage = "User with this Email already exists")]
public string Email { get; set; }
where "Account"
is the name of the serving controller and "UserAlreadyExistsAsync"
is it's action name.