admin管理员组

文章数量:1415420

According to select option I need to upload pdf or mp3/mp4.

If I select book or question paper, I need the validation to only to upload pdf or doc file,

If I select audio, I should upload mp3 only. If I select video, I should upload mp4 only.

Please help me to do this validation using javascript. I need just condition . the rest will be done through backend

<select id="upload_material" onchange="val()">
      <option>Select Material Type</option>
      <option value="1">Question Paper</option>
      <option value="2">Book</option>
      <option value="3">Audio/Video</option>
</select>
<label class="myLabel">
     <%= f.file_field :attachment, :onchange => "get_extension($this.value)" %>
     <!--  <input type="file" style="margin-left:80px;" name="filetwo"> -->
     <span>Browse</span>
</label>

According to select option I need to upload pdf or mp3/mp4.

If I select book or question paper, I need the validation to only to upload pdf or doc file,

If I select audio, I should upload mp3 only. If I select video, I should upload mp4 only.

Please help me to do this validation using javascript. I need just condition . the rest will be done through backend

<select id="upload_material" onchange="val()">
      <option>Select Material Type</option>
      <option value="1">Question Paper</option>
      <option value="2">Book</option>
      <option value="3">Audio/Video</option>
</select>
<label class="myLabel">
     <%= f.file_field :attachment, :onchange => "get_extension($this.value)" %>
     <!--  <input type="file" style="margin-left:80px;" name="filetwo"> -->
     <span>Browse</span>
</label>
Share Improve this question edited Oct 9, 2015 at 14:53 Gunaseelan 2,5425 gold badges41 silver badges44 bronze badges asked Oct 9, 2015 at 13:56 SkRoRSkRoR 1,1992 gold badges18 silver badges46 bronze badges 3
  • Provide the Javascript code that you tried – Gunaseelan Commented Oct 9, 2015 at 14:19
  • 2 Possible duplicate of Validation of file extension before uploading file – Gunaseelan Commented Oct 9, 2015 at 14:24
  • Client side validation is insecure, use server side validation. – Pedro Lobito Commented Oct 10, 2015 at 2:26
Add a ment  | 

1 Answer 1

Reset to default 6

You could use the javascript-test-function to test the filename and / or filetype, if it contains your desired filetypes.

Something like:

types = /(\.|\/)(mp3|mp4)$/i;
//file is the file, that the user wants to upload
file = data.files[0];

if (types.test(file.type) || types.test(file.name)) {
    alert("file is valid");
else{
    alert("file is invalid");
}

本文标签: htmlHow to validate mp4mp3 and PDF file using javascriptStack Overflow