1
votes

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
It is not a duplicate question. Here is the correct answer: codeproject.com/Questions/1261977/…user2868339

1 Answers

0
votes

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