Regular Expression Validator – display block rather than inline when dynamic

Is there any way to make a RegularExpressionValidator render itself using display:block, instead of display:inline in its style attribute, when setting the Display property to “Display=’Dynamic’”?

I have tried setting it in the stylesheet but this gets overwritten when it is rendered on the page.

Thanks

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The idea above on using a css with !Important was so close I could taste it. Using that idea and CSS attribute selectors 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.
span.valerror[style*="inline"]
{
    display:block !Important;
    background-color: Yellow;
    border: 1px solid #cccccc;
    font-size:.9em;
}

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.

Method 2

Just wrap the validator in a div:

<div><asp:RegularExpressionValidator id="x" runat="server"></div>

Method 3

I’ve found the only way to have the control not take up space when it is hidden and also display block is to put a <br /> tag after each validator.

So initially we have this:
enter image description here

Then if there is an error it looks like this:
enter image description here

Method 4

How about using !important in the CSS class?

Method 5

I’ve found a solution that solves this using a template control:

<asp:RequiredFieldValidator runat="server" EnableClientScript="True" Display="Dynamic" >
   <TemplateControl>
      <span class="error">This field is required.</span>
   </TemplateControl>
</asp:RequiredFieldValidator>

CSS:
.error{position:relative;display:block;}

The resulting html is a bit messy, but it allows a display:block that pushes the validation into the next line;
<span id="ctl00_###" style="color: red; display: inline; ">
   <templatecontrol>
      <span class="error">This field is required.</span>
   </templatecontrol>
</span>

Method 6

Works with .Display = ValidatorDisplay.Static for me, didn’t set EnableClientScript to true.

Update 1 and affecting cssClass with a class having display: block; to each regValidator

Update 2 forget about what I wrote before, I guess you don’t care now about this but for others I would say, I think it’s a forget of MS about regExpVal to not respond to display: block cause customValidator seems to work..

So for the regExpValidator I found that putting clear:left; and float:left works, and if the element under them moves while errors appears, you put clear: left on it.

Method 7

ASP.NET injects a javascript file with validation code, it’s the second script tag after the form tag in the HTML. This contains a function “ValidatorUpdateDisplay” that is called to show/hide validation messages. This can be overridden to use different javascript to show/hide e.g. if you are using jquery:

ValidatorUpdateDisplay = function (val) {
    // Show/hide this validator's error message
    if (val.isvalid){
        $(val).hide();
    } else {
        $(val).show();
    }
}

Or in your case:
ValidatorUpdateDisplay = function (val) {
    // Show/hide this validator's error message
    if (val.isvalid){
        val.style.display = 'none';
    } else {
        val.style.display = 'block';
    }
}

Simply put this code into a script tag after the ASP.NET form opening tag. Note this will affect all validators on the page, and ignores whether Display is set to Dynamic – if you wanted to support this you could extend it with code from the original function or custom code to check the type of validator.

Also see this question Can you have custom client-side javascript Validation for standard ASP.NET Web Form Validators?


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x