The @ Html.TextBoxFor text field is not highlighted after a failed check

I am using MVC3 with Razor. For input, I have two types of controls:

  • @Html.TextBoxFor
  • @Html.TextAreaFor

Both have a mandatory field check. @Html.TextAreaFor select the field if the verification is not completed, if there is no @Html.TextBoxFor .

Here is my code

HTML:

 @Html.TextBoxFor(m => m.FirstName) 

Model:

 [Required(ErrorMessage = "First Name is required")] public string FirstName { get; set; } 

Why doesn't the text box created with @Html.TextBoxFor highlight when its validation fails?

+7
source share
6 answers

Make sure that the variables that you assign to these input fields are [Required] in your model class. Also did you deprive any css during the "cleanup" process? Try adding this if it is not there.

 .field-validation-error { 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; } 
+8
source

I had this problem. I added a new CSS class to the Site.css file (or any other stylesheet that you prefer).

 textarea.input-validation-error { border: 1px solid #e80c4d; } 
+3
source

Today I faced the same problem:

To increase validity for a text field or any other field, you just need these two lines:

In the controller: ModelState.AddModelError("ErrorEmail", "Error Message");

In view: @Html.ValidationMessage("ErrorEmail")

And in web.config

 <appSettings> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> </appSettings> 

It was a rewrite of css with my own css website, so it did not show a red border around the text box.

make a validation error important my problem is resolved:

 input.input-validation-error { border: 1px solid #e80c4d !important; } 
+1
source

Add the error classes .input-validation-error and .input.input-validation-error.

0
source

I know this is old, but if someone else is looking for this, make sure you also use

 @Html.ValidationMessageFor(m => m.FirstName) 

After your text box

0
source

In order to have client-side validation, you need to make sure that you have the necessary scripts included in your view (whether through viewing _Layout.cshtml or direct view). Put this at the top of your view:

 <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 

Make sure the path and script names are correct. This will allow you to use client-side validation (i.e., Highlighting a text field).

-one
source

All Articles