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

Validate textbox based on ComboItem

2 Answers 77 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Daniel Plomp
Top achievements
Rank 2
Daniel Plomp asked on 02 Nov 2010, 03:47 PM
Hi,

I want to validate a textbox based on a specific value of a RadComboBox Item.
I am able to do this ServerSide with a CustomValidator, but I'd like to know how to do this ClientSide with a CustomValidator. I'm not that good with Javascript.

This is my ServerSide code:
protected void valGroup_ServerValidate(object source, ServerValidateEventArgs args)
{
   try
   {
      if (this.groups.SelectedValue == "*" && string.IsNullOrEmpty(this.group.Text))
         args.IsValid = false;
      else
         args.IsValid = true;
   }
   catch (Exception ex)
   {
    throw ex;
   }
}

Thanks,
Daniel

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 03 Nov 2010, 06:33 AM
Hello Daniel,
Try the following code snippet to perform client side validation with custom validator. Here is the example for your scenario.

ASPX:
<telerik:RadComboBox  ID="groups" runat="server">
</telerik:RadComboBox>
<telerik:RadTextBox ID="group" runat="server">
</telerik:RadTextBox>
<asp:CustomValidator ID="CV1" runat="Server"  ClientValidationFunction="JSValidate"
         Text="Enter correct Data" ControlToValidate="group" ValidateEmptyText="True"
         ErrorMessage="Validation Failed">
</asp:CustomValidator>

Javascript:
<script type="text/javascript">
    function JSValidate(source, args)
  {
        var combo = $find('<%=groups.ClientID %>');
        if (combo.get_selectedItem() == null)
        {
            args.IsValid = false;
            return;
        }
        var text = combo.get_selectedItem().get_text();   
        var element = document.getElementById('<%=group.ClientID %>');
        if (text == "*" && element.value == "")
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }
</script>

Thanks,
Princy.
0
Daniel Plomp
Top achievements
Rank 2
answered on 03 Nov 2010, 11:14 AM
Thanks Princy. I'll give it a try.

Regards,
Daniel
Tags
ComboBox
Asked by
Daniel Plomp
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 2
Daniel Plomp
Top achievements
Rank 2
Share this question
or