Multiple validation on one radtextbox.

4 posts, 1 answers
  1. Dan
    Dan avatar
    14 posts
    Member since:
    Jul 2013

    Posted 12 Nov 2013 Link to this post

    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?
  2. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 13 Nov 2013 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Dan
    Dan avatar
    14 posts
    Member since:
    Jul 2013

    Posted 19 Nov 2013 Link to this post

    Hi shinu. Need a clarification. Is there any special purpose for setting the error message twice?
  5. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 19 Nov 2013 Link to this post

    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.
Back to Top