在我的页面上我有一个单选按钮列表和一个文本框。CustomValidator文本框需要radiobuttonlist选择
默认情况下,该文本框处于隐藏状态,如果用户单击是,则会显示该文本框。一旦他们点击是,那么文本框必须填写,然后才能继续。
我已经把下面的代码,但它似乎并没有工作,当按下按钮进入下一页它继续而不是停止,并要求用户在框中输入的东西。
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 'Yes' 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" />
我有看如何做到这一点,但没有我看到了无论是工作,或者帮助我理解我要去哪里错了。
任何人都可以帮忙吗?
您是否需要自定义验证器?为什么不使用必需的字段验证程序?如果输入框不可见,则所需的字段验证程序不会尝试验证。 – 2013-03-04 12:12:56
从我所有的搜索它似乎自定义验证器是我需要的,因为我使用JavaScript来显示和隐藏文本框时,用户选择。我试图找到最有效的方法。 – saintsfanuk 2013-03-04 12:36:22
你能够使用jQuery吗? – 2013-03-04 12:55:33