Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8489625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:52:45+00:00 2026-06-10T21:52:45+00:00

I am using MVC 3 validation. My Product Manager wants the label for each

  • 0

enter image description here

I am using MVC 3 validation. My Product Manager wants the label for each control that has an error to turn red.

So ‘Student First Name’ label should turn red. ‘Email address’ label should turn red.

I tried to wrap each error msg in a div and check the length of each div

<div id="divValStudentFirstName">@Html.ValidationMessageFor(m => m.studentFirstName)</div>

in a js file:

$(document).ready(function () {

    if ($("#divValStudentFirstName").length > 1) {
                ("#divStudentFirstName").css("color", "red");

    }

But I have no success. The validation check is done without a complete refresh and as a result, my $(document).ready isn’t fired when validation hits.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T21:52:47+00:00Added an answer on June 10, 2026 at 9:52 pm

    Client side validation disabled :

    public static IHtmlString ValidationLabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string labelText = null) {
                var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
                var name = ExpressionHelper.GetExpressionText(expression);
                string resolvedLabelText = labelText ?? metadata.DisplayName ?? metadata.PropertyName ?? name.Split('.').Last();
                if (String.IsNullOrEmpty(resolvedLabelText)) {
                    return MvcHtmlString.Empty;
                }
    
                var tag = new TagBuilder("label");
                tag.Attributes.Add("for", TagBuilder.CreateSanitizedId(html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name)));
                tag.GenerateId(name);
                tag.SetInnerText(resolvedLabelText);
                ModelState modelState;
                string fullName = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
                if (html.ViewData.ModelState.TryGetValue(fullName, out modelState)) {
                    if (modelState.Errors.Count > 0) {
                        tag.Attributes.Add("style", "color:red");
                    }
                }
                return new MvcHtmlString(tag.ToString());
            }
    

    EDIT

    Client side validation enabled

    I’m really not a king in js, but this seems to work (well on a simple case at least)

    $('form').submit(function () {
            var form = $(this);
            $('label').removeClass('field-validation-error');
            if (!form.valid()) {
                $('.input-validation-error')
                    .each(function () {
                        $("label[for='" + $(this).attr("id") + "']").addClass('field-validation-error');
                    });
            }
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using MVC to create forms that are generated at runtime. For validation,
I am using a standard validation with MVC, acrossed with my fluent nHibernate [DisplayName(Product
I'm using ASP.NET MVC Validation. I want to know in my javascript functions whether
I'm using jQuery validation in ASP.net MVC. I would like to show validation summary
I'm using the DataAnnotations attributes along with ASP.Net MVC 2 to provide model validation
I am using asp.net mvc 2.0 and jquery validate 1.7 ( http://bassistance.de/jquery-plugins/jquery-plugin-validation/ ) What
I'm using MVC 3, and I'm trying to exclude some fields from validation in
I'm using ASP.NET MVC 3 code-first and I have added validation data annotations to
I am using MVC 3 + unobstrusive validation. For some field I am using
I'm using MVC 3 with unobtrusive javascript for client validation. I have a table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.