I have a field in my markup that I want to validate towards the server. My form currently looks like this:
<% using (Html.BeginForm()){ %>
<%: Html.Serialize("regData", Model)%>
<div class="RegistrationGroup">
<p><label for="Account_Email">e-mail</label> <%: Html.EditorFor(x => x.Account.Email) %><span class="ErrorMessage"></span></p>
</div>
<% } %>
Then in my jQuery script I have the following code:
$("form").validate({
rules: {
"Account.Email": {
required: true,
email: true
remote: "Registration/ValidateEmail"
}
}
});
In my RegistrationController class, I have the following method:
public string ValidateEmail(string email)
{
if (email.Contains("oek"))
return "false";
return "true";
}
So for testing, this should return false, and fail the validation if the e-mail address have "oek" somewhere inside it.
However, this does not work.
Without the remote part, the required and email rules work as they should, so I know that they are working.
When I add the remote rule, the following steps happen:
- The method on the controller gets called as planned, with the e-mail address as it's parameter.
- The return value from the method it intercepted inside the browser as true or false respectively ( I used Chrome's developers tools to control this).
But the validation does not work as planned at this point, and after the remote call is done, the required and email rules stop working as well. (it does not try to enforce the remote rule until the other rules have passed the validation, which is a good thing of course).
I have also tried to return a JsonResult with true or false, but to no avail.
What am I doing wrong here?