0

I have the following JQuery code in asp.net web form:-

  <script type="text/javascript">
     function ValidateFields(id) {
               $("#Rpt1").find("input[type=text]").each(function () {
                if ($.trim($(this).val()) == '') {
                    alert("At least one textbox is empty");

                    $(this).focus(); // focus the element
                    return false;
            }
          return false;

            })
         }

     </script>

      and on asp.net button click I have the following code:-
      <asp:Button ID="BtnSubmit" runat="server" OnClientClick="if 
      (ValidateFields('BtnSubmit') == false) return(false);" 
      OnClick="BtnSubmit_Click"  Text="Submit" />

The problem is it always fires the onclick event whether the condition is true or false.

I tried many things like OnClientClick="ValidateFields('BtnSubmit');" and OnClientClick="ValidateFields('BtnSubmit'); return false;"

but nothing works. The problem is in the code of jquery

Please help.

  • 1
    Possible duplicate of [OnclientClick and OnClick is not working at the same time?](https://stackoverflow.com/questions/2155048/onclientclick-and-onclick-is-not-working-at-the-same-time) – Se0ng11 Oct 01 '18 at 06:25
  • It is not a duplicate question. Here is the correct answer: https://www.codeproject.com/Questions/1261977/Onclientclick-not-working-in-ASP-NET – Madhukar Krishna Oct 01 '18 at 17:08

1 Answers1

0

Try this change

 <asp:Button ID="BtnSubmit" runat="server" OnClientClick=" return ValidateFields('BtnSubmit')" 
  OnClick="BtnSubmit_Click"  Text="Submit" />

if the ValidateFields function returns false then the onclick server event will not be executed.

Tell me if this worked

Amit chauhan
  • 540
  • 9
  • 22
  • 1
    No that didn't work. Sorry. Here is the correct answer:- https://www.codeproject.com/Questions/1261977/Onclientclick-not-working-in-ASP-NET – Madhukar Krishna Oct 01 '18 at 17:00