如果复选框已选中且文本框为空

本文关键字:文本 复选框 如果 | 更新日期: 2023-09-27 18:09:11

我有一个复选框,单击它的文本框被启用。我想验证如果复选框被选中用户不输入任何数据。它不应该允许用户提交表单。请参考javascript代码

Javascript:

<script type="text/javascript">
$(document).ready(function () {
    $('#chkCropLoan').change(function () {
        $("#txtAmount").prop("disabled", !$(this).is(':checked'));
        if (!$(this).is(':checked')) {
            $("#txtAmount").val("");
        }
    });
    $('#chkInvestmentLoan').change(function () {
        $("#txtInvestmentLoan").prop("disabled", !$(this).is(':checked'));
        if (!$(this).is(':checked')) {
            $("#txtInvestmentLoan").val("");
        }
    });
    $('#chkWarehouseReceipt').change(function () {
        $("#txtWarehouseReceipt").prop("disabled", !$(this).is(':checked'));
        if (!$(this).is(':checked')) {
            $("#txtWarehouseReceipt").val("");
        }
    });
    $('#chkFarmerProd').change(function () {
        $("#txtFarmerProd").prop("disabled", !$(this).is(':checked'));
        if (!$(this).is(':checked')) {
            $("#txtFarmerProd").val("");
        }
    });
});
</script>

文本框HTML代码:

<table>
    <tr>
        <td>Crop Loan</td>
        <td>
            <asp:CheckBox ID="chkCropLoan" runat="server" CssClass="check" onclick="javascript:enableTextBox();" />
            <asp:TextBox ID="txtAmount" runat="server" class="txtfld-popup1" MaxLength="5" Width="100" onkeypress="if(event.keyCode<48 || event.keyCode>57)event.returnValue=false;" Enabled="false"></asp:TextBox>
            <cc1:TextBoxWatermarkExtender ID="txtAmount_TextBoxWatermarkExtender" runat="server" TargetControlID="txtAmount" WatermarkText="Amount"></cc1:TextBoxWatermarkExtender>
            <asp:RegularExpressionValidator ID="rgfldvalidator" ControlToValidate="txtAmount"
                runat="server" ErrorMessage="Please enter the numbers only" ValidationExpression="^[0-9]*'.?[0-9]+$"></asp:RegularExpressionValidator>
        </td>
    </tr>
    <tr>
        <td>Investment Loan</td>
        <td>
            <asp:CheckBox ID="chkInvestmentLoan" runat="server" CssClass="check" OnChange="javascript:enableTextBox();" />
            <asp:TextBox ID="txtInvestmentLoan" runat="server" class="txtfld-popup1" MaxLength="5" width="100" onkeypress="if(event.keyCode<48 || event.keyCode>57)event.returnValue=false;" Enabled="false"></asp:TextBox>
            <cc1:TextBoxWatermarkExtender ID="txtInvestmentLoan_TextBoxWatermarkExtender" runat="server" TargetControlID="txtInvestmentLoan" WatermarkText="Amount"></cc1:TextBoxWatermarkExtender>
            <asp:RegularExpressionValidator ID="RegularExpressionValidator3" ControlToValidate="txtInvestmentLoan"
                runat="server" ErrorMessage="Please enter the numbers only" ValidationExpression="^[0-9]*'.?[0-9]+$"></asp:RegularExpressionValidator>
        </td>
    </tr>
    </tr>
    <tr>
        <td>Warehouse Receipt Finance</td>
        <td>
            <asp:CheckBox ID="chkWarehouseReceipt" runat="server" CssClass="check" OnChange="javascript:enableTextBox();" />
             <asp:TextBox ID="txtWarehouseReceipt" runat="server" class="txtfld-popup1" MaxLength="5" Width="100" onkeypress="if(event.keyCode<48 || event.keyCode>57)event.returnValue=false;" Enabled="false"></asp:TextBox>
            <cc1:TextBoxWatermarkExtender ID="txtWarehouseReceipt_TextBoxWatermarkExtender" runat="server" TargetControlID="txtWarehouseReceipt" WatermarkText="Amount"></cc1:TextBoxWatermarkExtender>
        </td>
    </tr>
    <tr>
        <td>Farmer Producer Companies</td>
        <td>
            <asp:CheckBox ID="chkFarmerProd" runat="server" CssClass="check" OnChange="javascript:enableTextBox();" />
            <asp:TextBox ID="txtFarmerProd" runat="server" class="txtfld-popup1" MaxLength="5" onkeypress="if(event.keyCode<48 || event.keyCode>57)event.returnValue=false;" Width="100" Enabled="false"></asp:TextBox>
            <cc1:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender11" runat="server" TargetControlID="txtFarmerProd" WatermarkText="Amount"></cc1:TextBoxWatermarkExtender>
        </td>
    </tr>
</table>

如果复选框已选中且文本框为空

尝试:

$("#your-form").submit(function() {
   if($(this).find("#your-text").val() && $(this).find("#your-check").checked){
      $("#your-form").submit();
   }
   else{
      // not checked or empty textbox
   }
   return false;
});

尝试使用下面的代码:

$('#your_form_name').submit(function( event ) {
        if ($('#chkCropLoan').is(':checked') && $('#txtAmount').val() == '') {
            alert('error message');
            event.preventDefault();
        }
});

如果文本框为空,则不希望提交表单

在提交按钮上写函数,点击事件

<input type="submit" value="save" onclick="return checkvalid()" />
在脚本

<script type="text/javascript">
    function checkvalid() {

        if (document.getElementById('textboxId').value==""
             || document.getElementById('textboxId').value==undefined) {
            return false;
        }
</script>