有谁知道如何确保在C#.net中提交时至少有一个电台被点击?
我有以下代码:
<asp:RadioButtonList ID="billingType" runat="server" CssClass="cl_billing_method"
RepeatDirection="Horizontal" data-messages="{required:'Billing methods is required'}">
<asp:ListItem Text="Email" Value="1"></asp:ListItem>
<asp:ListItem Text="Digital Mailbox" Value="2"></asp:ListItem>
<asp:ListItem Text="Paper" Value="0"></asp:ListItem>
</asp:RadioButtonList>
如何验证它,以便在点击提交按钮时,如果没有选择单选按钮,它将进行客户端检查并显示错误?
答案 0 :(得分:1)
<script type="text/javascript" language="javascript">
function Validate_Checkbox()
{
var chks=document.getElementsByTagName('input');
var hasChecked = false;
for (var i = 0; i < chks.length; i++)
{
if (chks[i].checked)
{
hasChecked = true;
break;
}
}
if (hasChecked == false)
{
alert("Please select at least one checkbox..!");
return false;
}
return true;
}
</script>
在提交按钮上你必须写
写
OnClientClick="return Validate_Checkbox()"
答案 1 :(得分:0)
更好的选择是使用asp:RequiredFieldValidator
,它将如下所示:
<asp:RequiredFieldValidator ErrorMessage="Billing methods is required"
ControlToValidate="billingType" runat="server" ValidationGroup="validateBill" />
将ValidationGroup
添加到按钮:
<asp:Button Text="Submit" ID="btnSubmit" runat="server" ValidationGroup="validateBill"/>
如果你收到一些错误,说&#34; UnobtrusiveValidationMode需要一个ScriptResourceMapping来实现&#39; jquery&#39; ...&#34; 然后将以下行添加到配置中:
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>