0

I'm aware of input-validation-error class added to elements bound to the model. But what if my input field is styled and wrapped with div:

<div class="inputWide">
    <span class="inputWideSpan"></span>
    @Html.TextBoxFor(m => m.FirstName, new { @class = "wide" })
</div>           

I would like to change/add styles to the inputWide div on the Model.FirstName validation fail to change background etc, is it possible to do ?

2 Answers 2

2

It really depends on whether the validation is client or server side. Better to do something like:

<div class="inputWide">
    @Html.LabelFor(m => m.CardNumber)
</div>
<div class="editor-field">
     @Html.TextBoxFor(m => m.FirstName, new { @class = "wide" })
     @Html.ValidationMessageFor(m => m.FirstName)
</div>

With css:

.field-validation-error 
{
    display: block;
    color: #ff0000;
}
.field-validation-valid {
    display: none;
} 
.input-validation-error {
    border: 1px solid #ff0000;
    background-color: #ffeeee;
}
.validation-summary-errors {
    font-weight: bold;
    color: #ff0000;
}
.validation-summary-valid {
    display: none;
}

Which will render something similar to:

results

Sign up to request clarification or add additional context in comments.

3 Comments

isn't that same thing and on validation failure additional css class will be added to the input field itself ?
but I'm interested on adding a css class to a wrapping div.
did you add any css yourself or those classes came with the project?
0

You'll need to use a plugin like jQuery Validate Hooks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.