Text Box Validator relating with combo box

2 posts, 0 answers
  1. : Arumuga Vignesh
    : Arumuga Vignesh  avatar
    21 posts
    Member since:
    Jul 2013

    Posted 28 Aug 2013 Link to this post

    Hi,

    I need to perform numerous validation for a  textbox depending upon combobox selection.
    For Eg:

    [combobox]
    <telerik:RadComboBox ID="rcbSearch" runat="server" Skin="Office2007">
            <Items>
                <telerik:RadComboBoxItem runat="server" Text="SELECT" Value="SELECT" />
                <telerik:RadComboBoxItem runat="server" Text="Check Number" Value="1" />
                <telerik:RadComboBoxItem runat="server" Text="Customer Name" Value="2" />
                <telerik:RadComboBoxItem runat="server" Text="File ID" Value="3" />
                <telerik:RadComboBoxItem runat="server" Text="Address"
                    Value="4" />
                <telerik:RadComboBoxItem runat="server" Text="City" Value="5" />
                <telerik:RadComboBoxItem runat="server" Text="State" Value="6" />
                <telerik:RadComboBoxItem runat="server" Text="Zip" Value="7" />
            </Items>
        </telerik:RadComboBox>

    [textbox]
    <telerik:RadTextBox ID="rtbSearch" runat="server" Skin="Office2007">
        </telerik:RadTextBox>

    Need to perform
    1. Numberic validation when File ID is selected from combobox.
    2. String validation when name or address is selected from combobox.
    3. Validate length of checknumber, city, zipcode separately.

    How it can be achieved?

    Thanks

  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 30 Aug 2013 Link to this post

    Hi Arumuga,

    You can use the ASP Custom Validator to achieve your requirement. Please have a look at the following code I tried to achieve your scenario.

    ASPX:
    <telerik:RadComboBox ID="rcbSearch" runat="server" Skin="Office2007">
        <Items>
            <telerik:RadComboBoxItem runat="server" Text="SELECT" Value="SELECT" />
            <telerik:RadComboBoxItem runat="server" Text="Check Number" Value="1" />
            <telerik:RadComboBoxItem runat="server" Text="Customer Name" Value="2" />
            <telerik:RadComboBoxItem runat="server" Text="File ID" Value="3" />
            <telerik:RadComboBoxItem runat="server" Text="Address" Value="4" />
            <telerik:RadComboBoxItem runat="server" Text="City" Value="5" />
            <telerik:RadComboBoxItem runat="server" Text="State" Value="6" />
            <telerik:RadComboBoxItem runat="server" Text="Zip" Value="7" />
        </Items>
    </telerik:RadComboBox>
    <telerik:RadTextBox ID="rtbSearch" runat="server" Skin="Office2007">
    </telerik:RadTextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Display="Dynamic"
        ErrorMessage="Required" ForeColor="Red" ControlToValidate="rtbSearch"></asp:RequiredFieldValidator>
    <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="rtbSearch"
        ForeColor="Red" ClientValidationFunction="validateText"></asp:CustomValidator>
    <br />
    <telerik:RadButton ID="RadButton1" runat="server" Text="Submit" CausesValidation="true">
    </telerik:RadButton>

    JavaScript:
    <script type="text/javascript">
        function validateText(sender, args) {
            var radcombobox = $find('<%=rcbSearch.ClientID %>');
            var selectedItem = radcombobox.get_selectedItem().get_text();
            var textboxvalue = args.Value;
            if (selectedItem == "File ID") {
                if (isNaN(textboxvalue)) {
                    args.IsValid = false;
                    sender.innerText = "File ID can contain only numeric values";
                    sender.textContent = "File ID can contain only numeric values";
                }
            }
            else if (selectedItem == "Customer Name" || selectedItem == "Address") {
                if (/[^a-zA-Z]/.test(textboxvalue)) {
                    args.IsValid = false;
                    sender.innerText = selectedItem + " can contain only alphabets";
                    sender.textContent = selectedItem + " can contain only alphabets";
                }
            }
            else if (selectedItem == "Zip") {
                if (textboxvalue.length != 7 || isNaN(textboxvalue)) {
                    args.IsValid = false;
                    sender.innerText = "Zip Code should be numbers of length 7";
                    sender.textContent = "Zip Code should be numbers of length 7";
                }
            }
        }
    </script>

    Please note that innerText property works in IE only and textContent works in Firefox only.

    Thanks,
    Shinu.
Back to Top