使用custion验证器基于radiobuttion选择进行验证 [英] validate based on radiobuttion selection using custion validator

查看:113
本文介绍了使用custion验证器基于radiobuttion选择进行验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想验证文本框长度是15当选择radiobuttion1时文本框长度是16当选择radiobuttion2时



我用过以下代码不起作用



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

<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>

推荐答案

您的控件是服务器端控件,您的验证代码是Javascript,因此您无法访问控件就像你在服务器端编码一样。

在你的Javascript中,试试这个来访问文本框:

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:
if (document.getElementById("TextBox2").value.length == 15)



将其与单选按钮一起使用:


Use this with your radio buttons:

if (document.getElementById("RadioButtonList1")[0].checked) 






and

if (document.getElementById("RadioButtonList1")[1].checked) 


<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 = 仅限数字 Font-Bold = True ForeColor = 红色 > < / asp:RegularExpressionValidator >



< asp:CustomValidator < span class =code-attribute> id = CustomValidator1

< span class =code-attribute> ControlToValidate = Text1

显示 = 静态

ErrorMessage = 输入有效数字!

ForeColor = green

< span class =code-attribute> 字体名称 = verdana

字体大小 = 10pt

OnServerValidate = ServerValidation

runat = 服务器 / >

< br / >

< asp:按钮 id = Button1

文本 = 验证

OnClick = ValidateBtn_OnClick

runat = server / >
" 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"/>


这篇关于使用custion验证器基于radiobuttion选择进行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆