Validate textbox based on ComboItem

3 posts, 0 answers
  1. Daniel Plomp
    Daniel Plomp avatar
    130 posts
    Member since:
    Feb 2004

    Posted 02 Nov 2010 Link to this post

    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. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 03 Nov 2010 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Daniel Plomp
    Daniel Plomp avatar
    130 posts
    Member since:
    Feb 2004

    Posted 03 Nov 2010 Link to this post

    Thanks Princy. I'll give it a try.

    Regards,
    Daniel
Back to Top