如果选中复选框并且文本框空白

时间:2014-06-19 07:13:37

标签: c# javascript checkbox

我有一个复选框,点击该复选框即可启用文本框。我想验证如果选中复选框并且用户没有放任何数据。它不应该允许用户提交表单。请参阅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>

3 个答案:

答案 0 :(得分:0)

尝试:

$("#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;
});

答案 1 :(得分:0)

尝试使用以下代码:

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

答案 2 :(得分:0)

如果文字框为空,则您不想提交表单

只需在提交按钮点击事件上编写功能

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