在asp.net中检查RadioButtonList和TextBox值的CustomValidator遇到问题
我有一个表单,其中包含一个名为“wmi\u手机”的文本框和一个RadioButtonList“wmi\u发送\u短信”。 基本上,我需要创建一个验证器来检查所选收音机的值是否为“Y”。如果是这种情况,则会检查TextBox值是否为空。如果为空,则应通知用户输入值 这是我的.aspx代码:在asp.net中检查RadioButtonList和TextBox值的CustomValidator遇到问题,asp.net,vb.net,textbox,radiobuttonlist,customvalidator,Asp.net,Vb.net,Textbox,Radiobuttonlist,Customvalidator,我有一个表单,其中包含一个名为“wmi\u手机”的文本框和一个RadioButtonList“wmi\u发送\u短信”。 基本上,我需要创建一个验证器来检查所选收音机的值是否为“Y”。如果是这种情况,则会检查TextBox值是否为空。如果为空,则应通知用户输入值 这是我的.aspx代码: <asp:TextBox ID="wmi_cell_phone" runat="server" MaxLength="100" Width="200px"></asp:TextBox>
<asp:TextBox ID="wmi_cell_phone" runat="server" MaxLength="100" Width="200px"></asp:TextBox>
<asp:RadioButtonList ID="wmi_send_sms" RepeatDirection="Horizontal" runat="server" Width="140px" CssClass="radio"></asp:RadioButtonList>
我的验证器
<asp:CustomValidator ID="val_wmi_send_sms" runat="server"
ClientValidationFunction="ValidateSMS"
Display= "Dynamic"
ErrorMessage="Please enter a valid phone number."> </asp:CustomValidator>
<script language="javascript" type="text/javascript">
function ValidateSMS(Source, args)
{
var smsRadio = document.getElementsByName('<%= wmi_send_sms.ClientID %>');
var cellphone = document.getElementById('<%= wmi_cell_phone.ClientID %>');
for (var x = 0; x < smsRadio.length; x ++)
{
if (smsRadio[x].checked)
{
if (smsRadio[x].value == "Y")
{
if (cellphone.value == "")
args.IsValid = false;
else
args.IsValid = true;
}
}
}
}
</script>
函数validateMs(源,参数)
{
var smsRadio=document.getElementsByName(“”);
var=document.getElementById(“”);
对于(变量x=0;x
但是它似乎不起作用。。可能我访问RadioButtonList的方式不对。如果验证程序调用ClientValidationFunction,您应该首先进行调试
- 为ie使用开发人员工具:
- Firefox的FireBug:
- ValidationGroup值是否在所有3个位置匹配:提交按钮、验证程序控件和要验证的控件
- 提交按钮是否具有CausesValidation=true
- RadioButtonList是否有原因验证=true
<table id="wmi_send_sms" class="radio" style="width:140px;">
<tr>
<td>
<input id="wmi_send_sms_0" type="radio" name="wmi_send_sms" value="Y" />
<label for="wmi_send_sms_0">Y</label>
</td>
<td>
<input id="wmi_send_sms_1" type="radio" name="wmi_send_sms" value="N" />
<label for="wmi_send_sms_1">N</label>
</td>
</tr>
</table>
然后,您可以通过访问
检查您的验证器中是否检查了“Y”一个,这意味着它似乎不起作用了
?您是否调试过js函数?也张贴你的验证器的标记。我的意思是,它没有做它应该做的。。。如果用户选择值为“是”的收音机,并将文本框留空,则应提醒用户他应该在文本框中输入值。。但是它没有做任何事情如果调用了js函数,你有没有调试过?ie使用开发者工具:Firefox使用FireBug:感谢Carson63000的支持,我非常感谢!不幸的是,我正在为我工作的公司的一位客户处理一个现有的项目。我无法更改应用程序的设计(因此我无法从RadioButtonList更改为RadioButton)。。我所能做的就是将验证器添加到现有代码中:(你知道这是怎么回事!很公平。好吧,稍微做点手脚,我找不到任何超级简单的方法来获取RadioButtonList中单个按钮的客户端ID。但是,它会让你得到一个表,你可以在其中查看-最终,你需要找到实际的元素,并查看哪个元素被选中。如果你可以的话,jQuery会让这变得更容易或者,您可以假设第一个的ID将是RadioButtonList的ClientID,末尾带有“\u 0”;即getElementsByName(“\u 0”),但我不能100%确定这是否安全或保证永远不会更改。
<table id="wmi_send_sms" class="radio" style="width:140px;">
<tr>
<td>
<input id="wmi_send_sms_0" type="radio" name="wmi_send_sms" value="Y" />
<label for="wmi_send_sms_0">Y</label>
</td>
<td>
<input id="wmi_send_sms_1" type="radio" name="wmi_send_sms" value="N" />
<label for="wmi_send_sms_1">N</label>
</td>
</tr>
</table>
<asp:RadioButton ID="wmi_send_sms_y" runat="server" GroupName="wmi_send_sms" Text="Y" />
<asp:RadioButton ID="wmi_send_sms_n" runat="server" GroupName="wmi_send_sms" Text="N" />