This is a migrated thread and some comments may be shown as answers.
Text Box Validator relating with combo box
1 Answer 13 Views
This is a migrated thread and some comments may be shown as answers.
: Arumuga Vignesh
Top achievements
Rank 1
: Arumuga Vignesh asked on 28 Aug 2013, 12:55 PM

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

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 1
answered on 30 Aug 2013, 06:32 AM
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.
Asked by
: Arumuga Vignesh
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 1
Share this question
or