Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. <input type="file" accept="image/png">
  2.  
  3. <form action="..."
  4. enctype=multipart/form-data method=post
  5. onsubmit="return checkPNG(document.getElementById('img'))">
  6. <label for=img>Your image (.png):</label>
  7. <input type=file id=img name=img accept=
  8. "image/png, .png" onchange="return checkPNG(this)">
  9. <input type=submit value=Send>
  10. </form>
  11. <div id=f></div>
  12. <script>
  13. function checkPNG(el) {
  14. if(el.value) {
  15. var parts = el.value.split('.');
  16. if(parts[parts.length - 1].toLowerCase() === 'png') {
  17. return true;
  18. } else {
  19. alert('Please specify a PNG file.');
  20. return false;
  21. }
  22. } else {
  23. return true;
  24. }
  25. }
  26. </script>
  27.  
  28. accept
  29. If the value of the type attribute is file, this attribute indicates the types of files that the server accepts; otherwise it is ignored. The value must be a comma-separated list of unique content type specifiers:
  30. A valid MIME type with no extensions
  31. audio/* representing sound files HTML5
  32. video/* representing video files HTML5
  33. image/* representing image files HTML5
  34.  
  35. function checkFileExt(el) {
  36. var accept=el.getAttribute("accept");
  37. if(el.value && el.value!="" && accept && accept!="") {
  38. var parts = el.value.split('.');
  39.  
  40. if(parts.length==1) {
  41. alert("File with no extension: '"+el.value+"'. Allowed: "+accept);
  42. return false;
  43. }
  44.  
  45. var ext=parts[parts.length - 1].toLowerCase();
  46.  
  47. accept=accept.split(',');
  48. var found=false;
  49. for(var i=0;i<accept.length;i++) {
  50. if("."+ext==accept[i]) found=true;
  51. }
  52. if(found) {
  53. return true;
  54. } else {
  55. alert("Wrong file: '"+el.value+"'. Allowed: "+accept);
  56. return false;
  57. }
  58. } else {
  59. return true;
  60. }
  61. }
  62.  
  63. <input name="fle_txt" value="" accept=".txt,.doc,.docx,.xls,.xlsx" onchange="checkFileExt(this);" type="file">
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement