ASP.NET WebForms: Short-circuit validation -
i have grid text box 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 numeric" /> <asp:comparevalidator runat="server" id="isnonnegative" type="double" operator="greaterthanequal" controltovalidate="txtmerchmin" amounttocompare="0" errormessage="* should non-negative"/> <asp:comparevalidator id="islessthanmax" controltovalidate="txtmerchmin" type="double" controltocompare="txtmerchmax" operator="lessthan" text="* should less max" runat="server"></asp:comparevalidator>
i validations run in following order , behave 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 code written right now, when value in txtmerchmin non-numbers, see error message of isnumbers, isnonnegative, , islessthanmax @ same time.
is there way "short-circuit" out of validation expected behavior?
just make customvalidator works on serverside , using if/else statements achieve behaviour. example:
<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>
in code behind in init
method set (you can in markup)
customvalidator.servervalidate += new servervalidateeventhandler(customvalidator_servervalidate);
and in function implement logic:
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; }
Comments
Post a Comment