Let me start by stating what I am really trying to do: I have to check a field for multiple "exclusive" (I'll explain) criteria, and display a single validation message.
Here is my code:
<asp:TextBox ID="Amount" runat="server" CssClass="field"></asp:TextBox>
<asp:RequiredFieldValidator Text="Amount is required"/>
<asp:CompareValidator Operator="DataTypeCheck" Type="Currency" Text="Amount must be numeric"/>
<asp:CompareValidator Operator="GreaterThanEqual" ValueToCompare="10" Type="Currency" Text="Minimum donation $10.00"/>
<asp:CompareValidator Operator="LessThan" ValueToCompare="10000" Type="Currency" Text="Wow that's too much"/>
I have left off some tags for readability - all validators are in the same validation group, they have ID's, ControlToValidate="Amount" Display="Dynamic"
.
If the Amount field is blank, or has a number in it, all is fine. However, if I put text in the field:
Even though the <10, >10,000 and DataTypeCheck should be exclusive, the numeric comparisons fail on the string.
I realize I could use a CustomValidator
for this, but I would like to eventually style one the messages differently, in its own separate <span>
element.
So, my incorrect (because they aren't "What am I really trying to do?") questions are:
- How do I make a
CompareValidator
of currency type not fail on text? or - How do I display only the first message from multiple validators? or
- How do I do what I want to do?