This is a migrated thread and some comments may be shown as answers.

Multiple validation on one radtextbox.

3 Answers 199 Views
Input
This is a migrated thread and some comments may be shown as answers.
Dan
Top achievements
Rank 1
Dan asked on 13 Nov 2013, 03:11 AM
I have a radtextbox in my page which is enabled when a raddropdownlist item is selected. The textbox is a required field once enabled and I want to perform multiple validation based on selected item of dropdownlist. Suppose first item is selected, the input should be validated for only numbers of a certain length. Suppose if item2 is selected,  user can enter alphanumeric characters. So how to switch the validation based on selected item?

3 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 13 Nov 2013, 06:04 AM
Hi Dan,

You can use an ASP Custom Validator to achieve your requirement. Please have a look into the following sample code I tried which works fine.

ASPX:
<telerik:RadDropDownList ID="RadDropDownList1" runat="server" DefaultMessage="Select"
    OnClientSelectedIndexChanged="OnClientSelectedIndexChanged">
    <Items>
        <telerik:DropDownListItem runat="server" Text="Item 1" />
        <telerik:DropDownListItem runat="server" Text="Item 2" />
    </Items>
</telerik:RadDropDownList>
<br />
<br />
<telerik:RadTextBox ID="RadTextBox1" runat="server" Enabled="false">
</telerik:RadTextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="RadTextBox1"
    ValidateEmptyText="true" ForeColor="Red" ClientValidationFunction="validateText"></asp:CustomValidator>

JavaScript:
<script type="text/javascript">
    function validateText(sender, args) {
        if ($find('<%=RadTextBox1.ClientID %>').get_enabled() == true) {
            var selectedItem = $find('<%=RadDropDownList1.ClientID %>').get_selectedItem().get_text();
            var textboxvalue = args.Value;
            if (selectedItem == "Item 1") {
                if (textboxvalue.length != 7 || isNaN(textboxvalue)) {
                    args.IsValid = false;
                    sender.innerText = "Value should be number of length 7";
                    sender.textContent = "Value should be number of length 7";
                }
            }
            else {
                var Exp = /^(\d+[a-z]|[a-z]+\d)[a-z\d]*$/i;
                if (!Exp.test(textboxvalue)) {
                    args.IsValid = false;
                    sender.innerText = selectedItem + " can contain only alphanumeric values";
                    sender.textContent = selectedItem + " can contain only alphanumeric values";
                }
            }
        }
    }
 
    function OnClientSelectedIndexChanged(sender, args) {
        var radtextbox = $find('<%=RadTextBox1.ClientID %>');
        radtextbox.enable();
    }
</script>

Thanks,
Shinu.
0
Dan
Top achievements
Rank 1
answered on 19 Nov 2013, 07:39 AM
Hi shinu. Need a clarification. Is there any special purpose for setting the error message twice?
0
Shinu
Top achievements
Rank 2
answered on 19 Nov 2013, 08:18 AM
Hi Dan,

The innerText property works fine in all browsers except older versions of Firefox. Firefox uses the textContent property instead. Please check this article.

Hope this helps,
Shinu.
Tags
Input
Asked by
Dan
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Dan
Top achievements
Rank 1
Share this question
or