RequiredFieldValidator places display:inline on the text

I have a RequiredFieldValidator with Display="Dynamic" on my ASP.NET WebForm. I have assigned it a class using the CssClass property. I want the error message to be displayed using display: block, so I have placed this on the css class in my style sheet.

Unfortunately, the validator places a display: inline on the element on the web page, effectivaly overriding my style sheet value.

Can I get rid of that?


I just realised why this doesn't work. When setting Display="Dynamic" on a validator, it has the effect that it sets style="display: none" on the span tag when rendering. The .net javascript library then switches the inline style of the element between none and inline. That is simply how the dynamic validator works. So for this to display as a block element, I will need to modify how the client side event validation works. Is it possible to do that?


Maybe this can help you:

Using CSS attribute selector and !important I did it. I had to use the "contains" selector to get it working in FF, but now I have tested it in IE10, FF and Chrome and so far it is working. It is really simple. Here is a sample validator in my aspx page:

<asp:RequiredFieldValidator runat="server" ID="rfvRequired" ErrorMessage="This is required.<br/>This is line 2" ControlToValidate="tbRequired" ValidationGroup="CommonAttributesValidationGroup" SetFocusOnError="True" CssClass="valerror" Display="Dynamic"></asp:RequiredFieldValidator>

Next I have a style for valerror.

    display:block !important;
    background-color: Yellow;
    border: 1px solid #cccccc;

That is it. How it works: when the span changes the style from "display:none" to "display:inline" the attribute selector on the span kicks in and forces it to be a block. You just need to make ONE CSS entry like the one above and make sure you make each validator that class.

An extremely hacky solution I once used (which I'm not proud of, but it worked) was to wrap the RequiredFieldValidator in a <div>, which is a block element; therefore even though your RequiredFieldValidator is inline, it's inside a block div so it'll effectively appear as display:block in most cases.

Told you it was hacky!

I have found a solution that works by overriding the .net ValidatorUpdateDisplay() method in JavaScript, and needs to be put before the close body tag.

<script type="text/javascript">
    function ValidatorUpdateDisplay(val)
        if (typeof (val.display) == "string")
            if (val.display == "None")
            if (val.display == "Dynamic")
       = val.isvalid ? "none" : "block";
        if ((navigator.userAgent.indexOf("Mac") > -1) && (navigator.userAgent.indexOf("MSIE") > -1))
   = "inline";
        } = val.isvalid ? "hidden" : "visible";

I was about to try a css solution, but after reading what you posted (updated), I think I may stick with mine. I already had to configure my validator on the server for other reasons, so I just check the control type of the "controlToValidate", then for textbox type controls, I add a <br /> tag to the front of the message.

e.g. // Inline (if configured) myvalidator.Text = "<br />My message";

// Normal message and validation summary (if configured) myvalidator.ErrorMessage = "My Message";

This keeps the line break from rendering in the validation summary, while still looking right for my inline messages.

I think Blackomen's approach is also good, but it needs to be selectively applied as well.

One option is to float the element to make the element act "more like a block".


<div class="form-group clearfix">
  <asp:CustomValidator runat="server" Display="Dynamic" CssClass="help-block" />

  float: left;
  width: 100%;

There is a simple solution which will work now and in the future.

1) Add a class to the validator 2) Use jquery to add an inner element to the validator span or use javascript

function wrapValidators() {

    $('.input-error').wrapInner('<span class="block" />');

3) add css to 'block' class "display:block"

Just set float:left to your css

By using above solution if your required field display before your control then simply add new line tag <br/> between your control and required field validator

Need Your Help

Checking a URL is valid (from php soap client)

php xdebug soap-client

I am writing a web app which will allow the user to specify a URL for a SoapClient. I wanted to validate that php can connect to the client when the user submits a form. I thouhgt I could do this v...