Reputation: 8237
I have an ajax form and inside i have an editor-for a model which has email Field with custom validation attribute which check if email already exist in the server so it goes like this:
$("#submitPop").live("click", (function (e) {
var form = $("#popForm");
form.validate();
if (form.valid()) {
$.ajax({
type: "POST",
url: "/Account/RegisterEmail/",
data: form.serialize(),
dataType: "json",
success: function (result) {
if (result.Status) {
location.reload(true);
} else {
alert("Something Went Wrong!"); //what should i write here to show the error message in its generated field-validation-error span
}
}
});
}
return false;
}));
inside the ajax form:
@Html.EditorFor(x => x.Email)
@Html.ValidationMessageFor(x => x.Email)</li>
as the comment says what do i when the ajax result return false in order to show the error message in the field-validation-error span generated?
i was thinking of a native way to inform jquery of the error and inform jquery to change all the needed changes like putting the red X and making the error span font red etc etc something like:
$.Validator.ShowErrorMessageFor("Email","Email is Already Taken")
Upvotes: 0
Views: 1656
Reputation: 3061
If you are using ASP.net MVC3 will suggest to use Remote Validation Attribute
Error message wil be rendered in Field Validation error Message Span.
ASP.NET MVC 3 provides a mechanism that can make a remote server call in order to validate a form field without posting the entire form to the server. This is useful when you have a field that cannot be validated on the client and is therefore likely to fail validation when the form is submitted. For example, many Web sites require you to register using a unique user ID. For popular sites, it can take several attempts to find a user ID that is not already taken, and the user's input is not considered valid until all fields are valid, including the user ID. Being able to validate remotely saves the user from having to submit the form several times before finding an available ID.
See this SO question remote-validation-in-asp-net-mvc-3- or refer MSDN Article
Upvotes: 2
Reputation: 2206
first, set an id to the span:
<span class="field-validation-error" id="validEmail"></span>
then you can use:
$('#validEmail').text('Email already exist...');
Upvotes: 0
Reputation: 79830
I assume that the Id of the span is field-validation-error
, then it should be like,
$('#field-validation-error').text('Email already exist, Please enter a different email ID');
Upvotes: 0