Javascript Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <script type= "text/javascript" > function chk(){ var fty= new Array( ".gif" , ".jpg" , ".jpeg" , ".png" ); // ประเภทไฟล์ที่อนุญาตให้อัพโหลด var a=document.form1.file.value; //กำหนดค่าของไฟล์ใหกับตัวแปร a var permiss=0; // เงื่อนไขไฟล์อนุญาต a=a.toLowerCase(); if (a != "" ){ for (i=0;i<fty.length;i++){ // วน Loop ตรวจสอบไฟล์ที่อนุญาต if (a.lastIndexOf(fty[i])>=0){ // เงื่อนไขไฟล์ที่อนุญาต permiss=1; break ; } else { continue ; } } if (permiss==0){ alert( "อัพโหลดได้เฉพาะไฟล์ gif jpg jpeg png" ); return false ; } } } </script> |
HTML code ใช้งาน
1 2 3 4 | < form action = "" method = "post" enctype = "multipart/form-data" name = "form1" id = "form1" onsubmit = "return chk();" > < input type = "file" name = "file" id = "file" /> < input type = "Submit" name = "button" id = "button" value = "Upload" /> </ form > |