Copy code code as follows:
function lastname(){
//Get the file path to upload
var filepath = document.getElementById("file1").value;
//To avoid problems with escaped backslashes, it will be converted here
var re = /(//+)/g;
var filename=filepath.replace(re,"#");
//Cut 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];
//Then intercept the file name to obtain the suffix name
var three=two.split(".");
//Get the last string intercepted, that is, the suffix name
var last=three[three.length-1];
//Add the type of suffix name to be judged
var tp ="jpg,gif,bmp,JPG,GIF,BMP";
//Return the position of the suffix name that meets the criteria in the string
var rs=tp.indexOf(last);
//If the returned result is greater than or equal to 0, it means that the file type allowed to be uploaded is included
if(rs>=0){
return true;
}else{
alert("The upload file you selected is not a valid picture file!");
return false;
}
}
Notes:
1. First save the script into a JS file and then include it in the page where the file is uploaded;
2. Add onsubmit="return lastname()" to the form of the upload page