function lastname(){
//Get the file to be uploaded Path
var filepath = document.getElementById("file1").value;
//In order to avoid problems with escaping backslashes, it will be converted here
var re = /(\ )/ g;
var filename=filepath.replace(re,"#");
//Cut and intercept the path string
var one=filename.split("#");
//Get the last one in the array, that is, the file name
var two=one[one.length-1];
//Intercept the file name to get the suffix name
var three=two. split(".");
//Get the last intercepted string, which is the suffix name
var last=three[three.length-1];
//Add the suffix name that needs to be judged Type
var tp="jpg,gif,bmp,JPG,GIF,BMP";
//Return the position of the qualified suffix name in the string
var rs=tp.indexOf(last) ;
//If the returned result is greater than or equal to 0, it means that it contains the file types that are allowed to be uploaded
if(rs>=0){
return true;
}else{
alert( "The uploaded file you selected is not a valid image file!");
return false;
}
}
Remarks:
1. First copy the script Save it as a JS file, and then include it in the page where you upload the file;
2. Add onsubmit="return lastname()" to the form on the upload page