2

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>
Gunaseelan
  • 2,137
  • 3
  • 30
  • 36
SreRoR
  • 976
  • 13
  • 34

1 Answers1

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");
}
Michael B
  • 1,579
  • 3
  • 22
  • 48