JavaScript: Check Upload File Extension
Filed in: Web Development — April 5th, 2004
Use: Simple JavaScript to check file extention for a form with file upload feature, so that user does not upload the wrong file type
Compatibility: Tested on Internet Explorer(IE) 6.0 and Mozilla 1.6. Should work for IE4+ and Netscape 4+
Code:
if((document.form1.upload.value.lastIndexOf(".jpg")==-1) {
alert("Please upload only .jpg extention file");
return false;
}
Explain: lastIndexOf will return the index of the last occurrence of the specified search argument. If not found, -1 will return. More detail…


