Click here to Skip to main content
15,896,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Friends,

I have a webpage with controls in it. This is a registration form.

Here, i want to validate Upload control using javascript.

I want to upload only PDF Files not other than these.

This is my Javascript code.

XML
<script type="text/javascript">
        function Validate() {
            var txt1 = document.getElementById('TxtName');
            var txt2 = document.getElementById('DDLReason');
            var txt3 = document.getElementById('DDLCondition');
            var uploadfile = document.getElementById('FileUploadTurnInRequest');

            if(txt1.value == '')
            {
                alert('Please enter Name');
                return false;
            }

             if(uploadfile.value == '')
            {
                alert('Please upload the attachment');
                return false;
            }


if(document.getElementById('<%=DDLReason.ClientID%>').selectedIndex == 0)
{
alert("Please Select Reason");
return false;
}

if(document.getElementById('<%=DDLcondition.ClientID%>').selectedIndex == 0)
{
alert("Please Select Condition");
return false;
}
 else
  {
  return confirm('Are you Sure, you want to Submit ?')
  }
    }
    </script>



Button
------
SQL
<asp:ImageButton ID="btnUpdate" runat="server" CausesValidation="true"
                                                                   ImageUrl="~/User/Images/Submit1.png" Width="180px" 
                                                                    Height="40px" OnClientClick="javascript:return Validate()"
                                                                   onclick="btnUpdate_Click"/>




Here, Just i need Validation using Javascript, Only to Upload PDF Files.

Please help
Posted

C#
function validation(thisform)
{
   with(thisform)
   {
      if(validateFileExtension(file, "valid_msg", "pdf files are only allowed!",
      new Array("pdf")) == false)
      {
         return false;
      }
      if(validateFileSize(file,1048576, "valid_msg", "Document size should be less than 1MB !")==false)
      {
         return false;
      }
   }
}

how-to-validate-upload-file-size-and-file-extension-using-javascript/[^]
how-do-i-validate-the-file-type-of-a-file-upload[^]
 
Share this answer
 
try this:-

C#
var uploadfile = document.getElementById('FileUploadTurnInRequest');

if (uploadfile .value.match(/pdf$/) || uploadfile .value.match(/PDF$/))
{
    return true;
}
else{return false;}


refer below links:-
http://stackoverflow.com/questions/71944/how-do-i-validate-the-file-type-of-a-file-upload[^]
http://stackoverflow.com/questions/190852/how-can-i-get-file-extensions-with-javascript[^]
 
Share this answer
 
try this..:)

XML
<asp:FileUpload ID="flUpld" runat="server" />

                <asp:RegularExpressionValidator
                    id="RegularExpressionValidator1" runat="server"
                    ErrorMessage="Only PDF files are allowed!"
                    ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.pdf|.PDF)$"
                    ControlToValidate="flUpld" CssClass="text-red"></asp:RegularExpressionValidator>


or

http://codingboys.blogspot.co.nz/2012/04/to-upload-pdf-files-only-using-aspnet.html[^]

http://www.aspdotnet-suresh.com/2010/12/how-to-validate-file-type-in-file.html[^]
 
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