I have a grid text box that I am validating:
<telerik:RadTextBox ID="txtMerchMin" runat="server" Text='<%# Bind("MerchandiseMinimumAmount") %>'></telerik:RadTextBox>
<asp:RequiredFieldValidator ID="required" runat="server" ErrorMessage="* required" ControlToValidate="txtMerchMin"></asp:RequiredFieldValidator>
<asp:CompareValidator runat="server" ID="isNumbers" Type="Double" Operator="DataTypeCheck" ControlToValidate="txtMerchMin" ErrorMessage="* must be numeric" />
<asp:CompareValidator runat="server" ID="IsNonNegative" Type="Double" Operator="GreaterThanEqual" ControlToValidate="txtMerchMin" AmountToCompare="0" ErrorMessage="* should be non-negative"/>
<asp:CompareValidator ID="isLessThanMax" ControlToValidate="txtMerchMin" Type="Double" ControlToCompare="txtMerchMax" Operator="LessThan" Text="* should be less than max" runat="server"></asp:CompareValidator>
I would like the validations to run in the following order and behave like so:
- If required validation fails, show required's error message only.
- If isNumbers validation fails, show isNumber's error message only.
- If isNonNegative validation fails, show isNonNegative's error message only.
- If isLessThanMax validation fails, show isLessThanMax's error message only.
As the code is written right now, when the value in txtMerchMin is non-numbers, I see the error message of isNumbers, isNonNegative, and isLessThanMax all at the same time.
Is there any way to "short-circuit" out of the validation to get my expected behavior?
Just make a CustomValidator that works on serverside and using if/else statements achieve your behaviour. For example:
In code behind in
init
method set (you can do this also in markup)And then in function implement your logic: