需要在单选按钮列表选择的CustomValidator文本框 [英] CustomValidator textbox required on radiobuttonlist select

查看:100
本文介绍了需要在单选按钮列表选择的CustomValidator文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网页我有一个单选按钮列表和一个文本框。

On my page I have a Radio button list and a textbox.

文本框默认是隐藏的然后出现如果用户点击是。一旦他们点击Yes,然后在文本框中必须填写他们可以再继续。

The textbox is hidden by default then appears if the user clicks yes. Once they click yes then the textbox must be filled in before they can proceed.

我已经把在以下code,但它似乎并没有在所有的工作,当按钮pssed去继续而不是停止并要求用户到下一个页面$ P $在框中输入一些东西。

I've put in the following code, however it doesn't seem to work at all and when the button is pressed to go to the next page it continues instead of stopping and asking the user to enter something in the box.

function Q12iYes() {           
        document.getElementById('txt12i').style.display = "block"
     }
    function Q12iNo() {
        document.getElementById('txt12i').style.display = "none"
    }      


<asp:RadioButtonList ID="rbtn12" runat="server" RepeatDirection="Horizontal">
    <asp:ListItem Value="Yes" onclick="Q12iYes()" /> <asp:ListItem Value="No"  Selected="True" onclick="Q12iNo()" /> 
    </asp:RadioButtonList>
            <br />

    12.i) If &#39;Yes&#39; please provide details<br />
    <br />
    <asp:TextBox ID="txt12i" runat="server" Height="62px" TextMode="MultiLine" Width="343px"></asp:TextBox>
            <br />                
            <asp:CustomValidator ID="MyTxtBoxValidator" runat="server" ControlToValidate="txt12i" ValidateEmptyText="true" ErrorMessage="Please enter some values into Textbox" 
            ClientValidationFunction="validateMyBtn"  Display="Dynamic"> </asp:CustomValidator>
              <script type="text/javascript">
                  function validateMyBtn(oSrc, args) {
                      var rbtnValue = null;
                      var rbtnList = document.getElementsByName('<%=rbtn12.ClientID %>');
                      var radio = rbtnList[0].getElementsByTagName("input");
                      for (var i = 0; i < radio.length; i++) { if (radio[i].checked) rbtnValue = radio[i].value; }
                      if (rbtnValue == "Yes") { args.IsValid = !(args.Value == "") }
                      else { args.IsValid = true; }
} 
</script> 

<asp:Button ID="btnContinue" runat="server" OnClick="btnContinue_Click" Text="Continue" /> 

我有看如何做到这一点,但没有我所看到的已经不是工作,或者帮助我理解我要去哪里错了。

I have had a look for how to do it, but nothing I have seen has worked either, or helped me understand where I'm going wrong.

谁能帮助?

推荐答案

A 的CustomValidator 不验证默认为空文,所以设置<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.validateemptytext.aspx\"相对=nofollow> ValidateEmptyText 属性:

A CustomValidator does not validate empty text by default, so set the ValidateEmptyText property:

<asp:CustomValidator  ValidateEmptyText="true" ID="MyTxtBoxValidator" runat="server" ...

修改的问题是你的JavaScript验证功能,仅在IE浏览器。下面是同样的作品在其他浏览器版本:

Edit The problem was your javascript validation function which works only in IE. Here's a version that works also in other browsers:

function validateMyBtn(oSrc, args) {
      var rbtnList = document.getElementsByName('<%=rbtn12.ClientID %>');
      var noClicked = false;
      for (var x = 0; x < rbtnList.length; x++) {
          if (rbtnList[x].checked) {
              noClicked = rbtnList[x].value == "No";
              break;
          }
      }
      if (noClicked) 
          args.IsValid = true;
      else
          args.IsValid = args.Value.trim().length > 0;
} 

这篇关于需要在单选按钮列表选择的CustomValidator文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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