I'm having trouble with my remote validation checking a second time after an invalid email has been given. I can't figure out why it's not removing the error after its no longer invalid.
Here is my Model:
[Required(ErrorMessage = "An email is required")]
[StringLength(150, MinimumLength = 4)]
[Display(Name = "Email")]
[RegularExpression("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$", ErrorMessage = "Invalid Email")]
[Remote("doesEmailExist", "Account", HttpMethod = "POST", ErrorMessage = "This email has already registered. Please use a different email")]
public virtual string strEmail { get; set; }
My View:
<ul>
<li class="editor-label">@Html.LabelFor(m => m.strEmail)</li>
<li class="editor-field">@Html.TextBoxFor(m => m.strEmail)</li>
<li class="error">@Html.ValidationMessageFor(m => m.strEmail)</li>
</ul>
My Controller:
[HttpPost]
public JsonResult doesEmailExist(string strEmail)
{
var user = db.t_user.FirstOrDefault(i => i.email == strEmail);
if (user == null)
return Json(null);
else
return Json(false);
}
Why is the remote validation not correcting itself going from an invalid entry to a valid entry?
Your
JsonReturn
need to returntrue
(notnull
) if no existing email exists. Note this really should be a GETor
Note also you can use
[EmailAddress]
in lieu of your[RegularExpression]
attribute