|
|
|
|
||||||||||||
ASP.NET Validation: Selective ValidationTeemu KeiskiWhen
developing applications and specifying controls that provide user input, one
might indeed face very customized needs to do the validation. Occasionally
validation should be selective and occasionally very restricted. In this
article I demonstrate how to develop custom validator
control that will help to achieve selective validation utilizing also client-side techniques so that certain
specified validator is enabled/disabled based on
input in other control.
1.
Derive
the validator from BaseValidator 2.
Create
“ValidatorToBeDisabled” property to specify ID of validator control, target validator,
that will be enabled/disabled based on selection 3.
Create
“DisableValue” property to specify the value when
target validation control is disabled 4.
Create
property to specify initial value that tells whether control to be validated is
considered to be “empty” ie. this custom validation
control will fail/pass validation 5.
Override
EvaluateIsValid method to specify server-side
validation logic. 6.
Override
ControlPropertiesValid method to specify whether
provided control ID is OK. 7.
Override
OnPreRender and AddAttributesToRender
methods to specify client-side validation logic when client browser is uplevel browser. Validation logic When the
value of control to be validated equals “DisableValue”,
the validator specified in “ValidatorToBeDisabled”
property is disabled and Boolean value True is returned from custom validator. If the value of control to be validated does not equal “DisableValue”,
it is compared to “InitialValue” to see if mandatory
selections/input has been specified for control to be validated. If not, in other words control value
equals “InitialValue”, target validator
is disabled and Boolean value False returned from our custom validator. Otherwise target validator
is enabled, validation done and Boolean value True returned from our custom validator. Usage example
<!-- Beginning of the page //--> <form id=WebForm6 method=post runat="server"><asp:RadioButtonList id=RadioButtonList1 runat="server"> <asp:ListItem>Work phone</asp:ListItem> <asp:ListItem>Home phone</asp:ListItem> <asp:ListItem Value="No
phone">No phone</asp:ListItem> </asp:RadioButtonList> <br> <cc2:SelectiveValidator id=SelectiveValidator1 runat="server" ControlToValidate="RadioButtonList1" DisableValue="No phone" ValidatorToBeDisabled="RequiredFieldValidator1" Display="Dynamic" ErrorMessage="Select the phone type"></cc2:SelectiveValidator> <asp:RequiredFieldValidator id=RequiredFieldValidator1 runat="server" ErrorMessage="You must
specify the phone number" ControlToValidate="TextBox1"></asp:RequiredFieldValidator> </form> <!-- End of the page //--> Demo
[Back to article index] | ||||||||||||||
| Copyright © 2000-2003 ASPAlliance.com Page Rendered at
12/2/2008 2:34:59 PM |
||||||||||||||