Click here to Skip to main content
15,908,437 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have javascript validation for file upload control on button OnClintclick event.
javascript function return false still button click event firing...how to avoid postback when javascript returns false?

thankyou

What I have tried:

My js code is
<script language="javascript" type="text/javascript">
        function checkRequiredFields() {


            var imgpath = document.getElementById('FileUpload1').value;
            if (imgpath == "") {
                alert("please select a file");
                document.file.FileUpload1.focus();
                return false;
            }
            else {
                alert("success");
            }

            var array = ['zip', 'jpg', 'jpeg', 'doc', 'docx', 'xls', 'xlsx', 'pdf', 'txt'];
            var xyz = document.getElementById("FileUpload1");
            var Extension = xyz.value.substring(xyz.value.lastIndexOf('.') + 1).toLowerCase();

            if (array.indexOf(Extension) <= -1) {
                alert("Selected file does not meet the upload criteria.");
                document.form.FileUpload1.focus();
                return false;
            }
             else {
                 alert("success");
            }

           }
       </script>


button control is
<asp:Button ID="CaseAttachments_btnUpload" runat="server" CssClass="FA_TextButton" Text="Upload" OnClientClick="return checkRequiredFields();" OnClick="MyButton_Click" />
Posted
Updated 19-Apr-16 2:49am
v2
Comments
Karthik_Mahalingam 19-Apr-16 8:10am    
Place debugger and check in which line you are getting error
ZurdoDev 19-Apr-16 8:15am    
Make certain it is returning false. Also, it seems like I've sometimes had to set UseSubmitBehavior to false for it to actually work. Not sure why. Perhaps try that as well.

1 solution

This Error will happen whenever there is an error in the javascript code ( syntax error or run time error )
If there is an error (Syntax or run time), and if unhanded it will return as true.
Try to do check the below :
Place a debugger in the first line of your javascript function
JavaScript
function checkRequiredFields() {
           debugger; // debugger to stop code execution on this particular line
           var imgpath = document.getElementById('FileUpload1').value;


To debug you can either use Chrome browser or IE by Enabling the Developer Tools, which can be done by
  • Chrome- Open the developer tools by pressing F12 or Menu->Tools (More tools) -> Developer tools
  • IE
  • - Press F12


Try to hit the function by some event ( say button click ). now you can debug line by line to Find where the exception has been raised.
The error will be displayed in the Console Tab of the Developer Tools window.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900