RadControls for ASP.NET AJAX
Validating entered values
The RadInput controls support ASP.NET validators (both client and server side). For example a RequiredFieldValidator can be used to make sure the user does not skip mandatory fields. A RangeValidator can be applied to ensure the entered value falls within some specified interval.
Note |
|---|
You can also use the MaxDate and MinDate properties on RadDateInput or the MaxValue and MinValue properties on RadNumericTextBox to restrict the input to a specified interval. |
To use ASP.NET validators with RadInput controls, simply set the ID of the RadInput control as the value of the ControlToValidate property of the validator.
Example
The following declaration shows the use of a required field validator from the form above:
Copy[ASP.NET] RequiredFieldValidator
<telerik:RadTextBox
id="RadTextBox1"
runat="server"
Skin="WebBlue">
</telerik:RadTextBox>
<asp:RequiredFieldValidator
ID="TextBoxRequiredFieldValidator"
Runat="server"
Display="Dynamic"
ControlToValidate="RadTextBox1"
ErrorMessage="The textbox can not be empty!" >
</asp:RequiredFieldValidator>The following declaration shows the use of a range validator from the form above:
Copy[ASP.NET] RangeValidator
<telerik:RadDateInput
ID="RadDateInput1"
style="font: 8pt monospace" runat="server"
DateFormat="d"
Skin="WebBlue"
MinDate="01/01/1990"
MaxDate="01/01/3000">
</telerik:RadDateInput>
<asp:RangeValidator
id="DateInputRangeValidator"
Runat="server"
ControlToValidate="RadDateInput1"
ErrorMessage="Choose a date between 5th of January 2005 and 1st of September 2005"
Display="Dynamic"
MaximumValue="2005-09-01-00-00-00"
MinimumValue="2005-01-05-00-00-00" >
</asp:RangeValidator>For a live example of the form shown above, see the Validation demo.
Triggering validation
If the validators are set to use client-side validation (their EnableClientScript property is True), the RadInput controls automatically trigger the validator that checks their value when the user finishes editing. That is, when the user has changed the value in the control and it then loses focus, the validator checks the new value of the input control.
You can also use the RadInput controls to trigger the validation of other controls on the page. To make a RadInput control trigger other validators when it causes a postback:
Set the AutoPostBack property to True. When AutoPostBack is True, the input control causes a postback when the user changes its value and it then loses focus.
Set the CausesValidation property to True. This causes the postback that the RadInput control initiates to trigger the validators on the Web page (not just the validator that is checking the value of the RadInput control.)
By default, all validators on the Web page must be successful before the postback that the input control initiates can occur. You can prevent some controls from being validated on the postback while requiring other controls on the Web page to be validated. This is accomplished by using Validation Groups.
The validator controls have a ValidationGroup property. The RadInput controls also have a ValidationGroup property. When the input control causes a postback and triggers the validators on the page, it only causes validation by those validators whose ValidationGroup property matches the ValidationGroup property of the input control. (The reason the default behavior is for all validators to execute on postback is because the default value of the ValidationGroup property on both the input control and the validators is an empty string.)
See Also