Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Red border around TextBox when validation fails

I am using ASP.NET MVC 2.

Html.DropDownListFor and Html.TextAreaFor automatically get red borders when the validation fails.

How to make the four borders of a TextBox (using Html.TextBoxFor) red when it fails validation?

For example, I have a TextBox that is required and when the user submits the form without specifying a value in the textbox, I want the textbox to have red borders.

like image 851
thd Avatar asked May 13 '10 21:05

thd


People also ask

How do you make a text box red border validation fails in bootstrap?

Try this. use <form name="preSourceThresholdForm" novalidate class="form-horizontal"> instead of <form name="preSourceThresholdForm"> . you have included bootstrap css?


2 Answers

When validation fails for a model property - it'll add a class to the input in the html. Have a look at the rendered html when the validation fails (using view source or firebug) and check out the class for your input*. Then edit your css to include a style for the failed validation.

E.g. In my project I have:

input.input-validation-error,
textarea.input-validation-error,
select.input-validation-error
{
    background: #FEF1EC;
    border: 1px solid #CD0A0A;
}

HTHs,
Charles

* I'm pretty sure ASP.NET MVC add the class input-validation-error by default.

like image 81
Charlino Avatar answered Oct 17 '22 18:10

Charlino


All you need to do is the CSS below:

.input-validation-error {
    border: 1px solid #ff0000;
    background-color: #ffeeee;
}
like image 24
Qasim Bataineh Avatar answered Oct 17 '22 16:10

Qasim Bataineh