Click here to Skip to main content
15,879,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i want to validate a textbox length is 15 when radiobuttion1 is selected and textbox length is 16 when radiobuttion2 is selected

I used the following code it's not worked

XML
<asp:TextBox ID="TextBox1" runat="server" MaxLength="16" ></asp:TextBox>
<asp:CustomValidator ID="CustomValidator2" runat="server"   ControlToValidate="TextBox2" ErrorMessage="Not Valid Number" ClientValidationFunction="validcard" ></asp:CustomValidator>

 <script type="text/javascript">
    function validcard()
    {
     var isValid = false;
     if (RadioButtonList1[0].checked == true)
     {
        if (Textbox2.length == 15)
        {
            isValid = true;
        }
        else
        {
            isValid = false;
        }

      }
      else if (RadioButtonList1[1].checked == true)
      {
       if (Textbox2.length == 16)
       {
          isValid = true;
       }
       else
       {
          isValid = false;
       }

      }
      else isValid = false;


         return isValid;
     }
</script>
Posted

Your control is server side controls and your validation code is in Javascript, so you cannot access to the control like when you're coding at server side.
In your Javascript, try this to access the textbox:
JavaScript
if (document.getElementById("TextBox2").value.length == 15)

Use this with your radio buttons:
JavaScript
if (document.getElementById("RadioButtonList1")[0].checked) 

and
JavaScript
if (document.getElementById("RadioButtonList1")[1].checked) 
 
Share this answer
 
Comments
surendra4u 14-Aug-13 9:33am    
Thanks for your help, but still in same, i think at "CustomValidator" is wrong for using function (validcard), can you suggest the code for my requirement (contorls are server side )
XML
<script runat="server">

      void ValidateBtn_OnClick(object sender, EventArgs e)
      {
         // Display whether the page passed validation.
         if (Page.IsValid)
         {

             myfun();
         }
      }

      void ServerValidation(object source, ServerValidateEventArgs args)
      {
          string txt;
          txt=Text1.Text;
          if (RadioButtonList1.SelectedValue == "1")
          {
              if (txt.Length == 15)
                  args.IsValid = true;

              else
                  args.IsValid = false;

          }
          else if (RadioButtonList1.SelectedValue == "2")
          {
              if (txt.Length == 16)
                  args.IsValid = true;

              else
                  args.IsValid = false;
          }
          else args.IsValid = false;
      }

   </script>


   ///////////////////////////////

   <asp:RadioButtonList
              ID="RadioButtonList1" runat="server">
          <asp:ListItem Value="1">amex</asp:ListItem>
          <asp:ListItem Value="2">other</asp:ListItem>
   </asp:RadioButtonList>

    <asp:TextBox id="Text1" runat="server" />
    <asp:RequiredFieldValidator ID="RFVdob" runat="server" ControlToValidate="Text1" ErrorMessage="Enter Card number" Font-Bold="True" ForeColor="Red"></asp:RequiredFieldValidator>
    <asp:RegularExpressionValidator ID="REt1" runat="server" ControlToValidate="Text1"    ValidationExpression="^\d+$" ErrorMessage="Numbers only " Font-Bold="True" ForeColor="Red" ></asp:RegularExpressionValidator>



   <asp:CustomValidator id="CustomValidator1"
           ControlToValidate="Text1"
           Display="Static"
           ErrorMessage="Enter valid number!"
           ForeColor="green"
           Font-Names="verdana"
           Font-Size="10pt"
           OnServerValidate="ServerValidation"
           runat="server"/>

      <br />

      <asp:Button id="Button1"
           Text="Validate"
           OnClick="ValidateBtn_OnClick"
           runat="server"/>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900