Hi valentinaorju,
Note: For this sample I have refer below articles. For more details refer below article links.
Please refer below sample.
Model
public class PersonModel
{
[Required(ErrorMessage = "Email is required.")]
[RegularExpression(@"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$",
ErrorMessage = "Invalid Email Address.")]
public string Email { get; set; }
}
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(PersonModel person)
{
return View();
}
[HttpPost]
public JsonResult CheckEmail(string email)
{
dbUsersEntities entities = new dbUsersEntities();
bool isValid = entities.Users.ToList().Exists(p => p.Email.Equals(email, StringComparison.CurrentCultureIgnoreCase));
return Json(isValid);
}
}
View
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<span>Email:</span>
<input id="txtEmail" type="text" name="Email" onkeyup="CheckAvailability()" />
@Html.ValidationMessageFor(m => m.Email, "", new { @class = "error" })
<span id="message"></span><br />
<input id="btnCheck" type="submit" value="Submit" />
}
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
function CheckAvailability() {
$(".field-validation-error").hide();
$("#message").hide();
var email = $("#txtEmail").val();
$.ajax({
type: "POST",
url: "/Home/CheckEmail",
data: '{email: "' + email + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
$("#message").show();
var message = $("#message");
if (!response) {
//Email available.
message.css("color", "green");
message.html("Email is available");
} else {
//Email not available.
message.css("color", "red");
message.html("Email is NOT available");
}
}
});
};
</script>
Screenshot