Copy codeThe Code is as follows:
Function lastname (){
// Obtain the path of the file to be uploaded
Var filepath = document. getElementById ("file1"). value;
// To avoid escape backslash problems, convert it here
Var re =/(\ +)/g;
Var filename = filepath. replace (re ,"#");
// Cut the path string
Var one = filename. split ("#");
// Obtain the last object name in the array.
Var two = one [one. length-1];
// Truncate the file name to get the suffix
Var three = two. split (".");
// Obtain the last intercepted string, that is, the suffix
Var last = three [three. length-1];
// Add the suffix type to be determined
Var tp = "jpg, gif, bmp, JPG, GIF, BMP ";
// Return the position of the qualified Suffix in the string
Var rs = tp. indexOf (last );
// If the returned result is greater than or equal to 0, it indicates the file type that can be uploaded.
If (rs> = 0 ){
Return true;
} Else {
Alert ("the uploaded file you selected is not a valid image file! ");
Return false;
}
}
Note:
1. Save the script as a JS file and include it on the page of the uploaded file;
2. Add onsubmit = "return lastname ()" to the form on the upload page ()"