简体   繁体   English

我如何确定提交时至少点击了一个电台

[英]How can I make sure that they are at least one radio been click when submit

Does anyone know how can I make sure that they are at least one radio been click when submit in C# .net? 有谁知道我如何确保在C#.net中提交时至少单击了一个广播?

I have the following code: 我有以下代码:

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

How can I validate it so that when submit button is been clicked and it will do the client check and show error if there have no radio button been selected? 我如何验证它,以便在单击“提交”按钮后,如果没有选择任何单选按钮,它将对客户端进行检查并显示错误?

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

and on Submit Button you have to write 在“提交”按钮上,您必须编写

write

OnClientClick="return Validate_Checkbox()"

Better option is use a asp:RequiredFieldValidator it will looks like this: 更好的选择是使用asp:RequiredFieldValidator它看起来像这样:

<asp:RequiredFieldValidator ErrorMessage="Billing methods is required"
 ControlToValidate="billingType" runat="server" ValidationGroup="validateBill"  />

Add ValidationGroup to the button: ValidationGroup添加到按钮:

<asp:Button Text="Submit" ID="btnSubmit" runat="server" ValidationGroup="validateBill"/>

If you are getting some error saying "UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery' ..." then add the following lines to the config: 如果出现错误,提示“ UnobtrusiveValidationMode需要'jquery'使用ScriptResourceMapping ...”,则将以下几行添加到配置中:

<appSettings>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当ItemsSource为空时,如何确保WPF DataGrid至少显示一行? (可编辑的ComboBox单元格) - How can I make sure my WPF DataGrid shows at least one row when ItemsSource is empty? (Editable ComboBox cells) 当我点击销毁其中一个时,如何确保我的所有块都不会被销毁? - How do I make sure that all of my blocks don't get destroyed when I click to destroy one of them? 我如何确保只有一个线程可以执行某些操作? - How can I make sure that exactly one thread will do something? 如何至少在显示窗体的框架之前不加载窗体的控件? - How can I make a form's controls not load until at least the frame of the form has been displayed? 如何确保在按钮的单击事件之前触发文本框的textchanged事件? - How can I make sure the textchanged event of a textbox is fired before the click event of a button? 统一如何设置文本高度以确保至少显示一行 - In unity, how to set Text height to make sure at least show one line 如何确保返回是一个数组? - How can I make sure the return is an array? 如何确保单击时UI更新为正确的图像? - How do I make sure the UI updates to the correct image on click? 如何确保一次只打开一个WPF窗口? - How can I make sure only one WPF Window is open at a time? 如何确保已单击该按钮? - How to make sure that button has been clicked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM