I am trying to check data before posting but on button click the post is running no matter the result of the function. I have
button
<asp:Button ID="btnSave" Text="Save" OnClientClick="if (!CheckData()) { return false; }" OnClick="Adauga_Raspunsuri" runat="server" class="btn btn-md lfc-red ml-1 mr-1" style="font-size:14px;width:150px;" ClientIDMode="Static" />
and jquery
<script type="text/javascript"> $(document).ready(function () { $("#txtCorect1").val($("#ddlCorect1 option:selected").text()); $("#txtCorect2").val($("#ddlCorect2 option:selected").text()); $("#txtCorect3").val($("#ddlCorect3 option:selected").text()); $("#ddlCorect1").on("change", function () { var c1 = $("#ddlCorect1 option:selected").text(); $("#txtCorect1").val(c1); }) $("#ddlCorect2").on("change", function () { var c2 = $("#ddlCorect2 option:selected").text(); $("#txtCorect2").val(c2); }) $("#ddlCorect3").on("change", function () { var c3 = $("#ddlCorect3 option:selected").text(); $("#txtCorect3").val(c3); }) $(".colorchg").each(function () { $(this).css("background", $(this).val()); }); $(".colorchg").change(function () { $(this).css("background", $(this).val()); }); $("#txtRaspuns1").on("change", function () { var r1 = $("#txtRaspuns1").val().length; }) $("#txtRaspuns2").on("change", function () { var r2 = $("#txtRaspuns2").val().length; }) $("#txtRaspuns3").on("change", function () { var r3 = $("#txtRaspuns3").val().length; }) }); function CheckData() { var raspuns = false; var corect = false; if (r1 == 0 || r2 == 0 || r3 == 0) { raspuns = false; } else { raspuns = true; } if (c1 == "DA" && c2 == "NU" && c3 == "NU") { corect = true; } else if (c1 == "NU" && c2 == "DA" && c3 == "NU") { corect = true; } else if (c1 == "NU" && c2 == "NU" && c3 == "DA") { corect = true; } else { corect = false; } alert(corect); alert(raspuns); if (raspuns == true && corect == true) { return true; } else { return false; } } </script>
So, no matter if c1, c2 and c3 are all "NU", or all "DA" or r1, r2, r3 are empty, OnClick="Adauga_Raspunsuri" is run. And I can't find why