I have server side validation messages for some of my text fields. The field 'title' is a required field server side with a '[Required]' data attribute in the class, but this only seems to get checked on a postback or form submit. I'm saving the 'title' text field on a jquery .change event, sudo code below. But I want to use my validationmessagefor to show the error message from the client side check. Not sure how to go about doing this?
html.
@Html.ValidationMessageFor(model => model.Overview.Title, , new { @class = text-danger })
rendered as
<span class=field-validation-valid text-danger data-valmsg-for=Overview.Title data-valmsg-replace=true></span>
If I want to do some client side checking in javascript and then use this element to display a client side error message can I do so. I'm just trying to save myself of using another hidden element. I know I would have to use
data-valmsg-for=Overview.Title
because the other data attributes are the same for all the other text fields.
Whats the best way to do display client side error messages here if I want to check to make sure the title has length greater then 1?
Maybe something like -
$('#Overview_Title').change(function() {
var title = $(this).val();
if (title.length == 0) {
// show error message title has to be a value to save
}
else {
// do other work like save to db
}
});