Link to home
Start Free TrialLog in
Avatar of elliottbenzle
elliottbenzle

asked on

using two asp.net validation controls

In my form I need to validate a field for both required and for range.

The problem is that if the second validation control message is pushed far to the left, because of the 1st validation control message. The code looks like this:


<asp:TextBox ID="txtClass" Width="80px" runat="server"></asp:TextBox>
<asp:RangeValidator ID="RangeValidator1" runat="server" ErrorMessage="Enter a valid year." ControlToValidate="txtClass" MaximumValue="2200" MinimumValue="1800" SetFocusOnError="True" Visible="True" Display="Static"></asp:RangeValidator>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Enter a valid graduation date." ControlToValidate="txtClass" Visible="False"></asp:RequiredFieldValidator>

And if no value is entered the error message appears far off to the left, like this

TEXT FIELD                                                                                         Enter a graduation date

This appears to be because the first validation control is still taking up space even when not visible.

Does anyone know a work around for this problem?

Thanks

Example page is here: http://www.glowfishtw.com/stlukes/alumni.aspx

Avatar of Paul Jackson
Paul Jackson
Flag of United Kingdom of Great Britain and Northern Ireland image

Use the ValidationSummaryControl to show the error message and use the text property of the validator controls to show an *

Have a look here : http://msdn.microsoft.com/en-us/library/ms972961.aspx
Try the below settings
<asp:RangeValidator ID="RangeValidator1" runat="server" ErrorMessage="Enter a valid year." ControlToValidate="txtClass" MaximumValue="2200" MinimumValue="1800" SetFocusOnError="True" Visible="True" Display="None"></asp:RangeValidator>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of mayank_joshi
mayank_joshi
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
using Display="Dynamic"  the validation control will occupy space only in case of incorrect entry.
Avatar of elliottbenzle
elliottbenzle

ASKER

Thanks. That was it.