我有一个我正在验证的网格文本框:
<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>
我希望验证按以下顺序运行,并且表现如下:
由于代码是现在编写的,当txtMerchMin中的值是非数字时,我会同时看到isNumbers,isNonNegative和isLessThanMax的错误消息。
有没有办法从验证中“短路”以达到预期的行为?
答案 0 :(得分:0)
只需创建一个在服务器端工作的CustomValidator,并使用if / else语句实现您的行为。 例如:
<telerik:RadTextBox ID="txtMerchMin" runat="server" Text='<%# Bind("MerchandiseMinimumAmount") %>'></telerik:RadTextBox>
<asp:RequiredFieldValidator ID="required" runat="server" ErrorMessage="* required" ControlToValidate="txtMerchMin"></asp:RequiredFieldValidator>
<asp:CustomValidator runat="server" ID="customValidator" Display="Dynamic" SetFocusOnError="true" ControlToValidate="txtMerchMin"></asp:CustomValidator>
在init
方法集中的代码中(您也可以在标记中执行此操作)
customValidator.ServerValidate += new ServerValidateEventHandler(customValidator_ServerValidate);
然后在函数中实现你的逻辑:
protected void customValidator_ServerValidate(object source, ServerValidateEventArgs args)
{
bool isValid = true;
double price;
bool isDouble = Double.TryParse(args.Value, out price);
if(!isDouble) {
// not double (numeric)
isValid = false;
}
else if (...)
else if (...)
args.IsValid = isValid;
}