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
1 Answer
Reset to default 6You 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
版权声明:本文标题:html - How to validate .mp4, .mp3 and PDF file using javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745236345a2649061.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论