Home > Software engineering >  With ASP.NET, how do I make display names of properties show in lower case in validation error messa
With ASP.NET, how do I make display names of properties show in lower case in validation error messa

Time:11-17

In ASP.NET, what is the best way to display field/property names in form validation error messages in lower case?

For example, if I have a Price property on a model, and Price is not nullable, and I leave the Price field blank when filling out a form for this model, then I will get this error message on the form:

The Price field is required.

"Price" is capitalised. Is there any easy way to make it lower case, like the following?

The price field is required.

There must be a way of making these property names show in lower case for every error message. Because yes, if I just wanted it for one property, then I could set a custom error message using the Required attribute:

[Required(ErrorMessage = "The price field is required.")]
public decimal Price { get; set; }

But I'm wondering if there is a way to make these property names show in lower case by default for every error message?

I did find this question with some answers, but the solutions seem pretty complex, and also that person is talking about JSON serialisation, which is a bit different to my case.

Thanks if anyone can share any info on this problem.

CodePudding user response:

If you use ValidationMessageFor on the page like this

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

It will show the error message in a span with the class field-validation-error. So if you now the class you can use css to transform the text to lowercase.

<style>
    .field-validation-error {
        text-transform: lowercase;
    }
</style>

CodePudding user response:

You can try this:

<style>
    .field-validation-error {
        text-transform: lowercase;
    }
    .field-validation-error:first-letter {
        text-transform: uppercase;
    }
</style>
  • Related