How do I make a checkbox required on an ASP.NET form?

后端 未结 6 1237
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 02:51

I\'ve done some searching on this, and I\'ve found several partial answers, however nothing that gives me that warm fuzzy \"this is the right way to do this\". To answer the

6条回答
  •  庸人自扰
    2020-11-28 03:27

    javascript function for client side validation (using jQuery)...

    function CheckBoxRequired_ClientValidate(sender, e)
    {
        e.IsValid = jQuery(".AcceptedAgreement input:checkbox").is(':checked');
    }
    

    code-behind for server side validation...

    protected void CheckBoxRequired_ServerValidate(object sender, ServerValidateEventArgs e)
    {
        e.IsValid = MyCheckBox.Checked;
    }
    

    ASP.Net code for the checkbox & validator...

    
    You must select this box to proceed.
    

    and finally, in your postback - whether from a button or whatever...

    if (Page.IsValid)
    {
        // your code here...
    }
    

提交回复
热议问题