How can I get the value of Validation Message from form in ASP.Net MVC, using javascript?
Hy,
In my ASP.NET MVC application in the aspx page I set
<% Html.EnableClientValidation(); %>
to enable client validation on my form. So, for my input I have this code to declare the texbox for email and it's validation
<%: Html.TextBoxFor(model => model.Email)%>
<%: Html.ValidationMessageFor(model => model.Email)%>
<%: Html.ValidationSummary(true)%>
In the model I declare the email property like:
[Required(ErrorMessage = "Required email")]
[DisplayName("Email *")]
[RegularExpression("^[a-z0-9+\\+-]+(\\.[a-z0-9+\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2,4})$", ErrorMessage = "Invalid email.")]
public string Email { get; set; }
The question is, how can I get (using client script) the value of the error message when it's displayed on the page?
So when I press some button I can see if the form is valid or not and after that I can display a loading gif.
Thanks a lot.
Jeff
Update
HTML source code:
<tr>
<td class="editor-field">
<input id="Email" name="Email" type="text" value="" />
</td>
</t开发者_StackOverflowr>
<tr>
<td class="editor-field-validation">
<span class="field-validation-valid" id="Email_validationMessage"></span>
<div class="validation-summary-valid" id="validationSummary"><ul><li style="display:none"></li>
</ul></div>
</td>
</tr>
The value of error message (text) will probably displayed in div or span. Simply check what will be the id of that field (for example with FireBug). And then you will be able to get the value of that when you'll need it.
Because I understand that you are calling validation manualy in JavaScript, yes?
精彩评论