Home > Web Front-end > JS Tutorial > body text

js gets the sample code to determine the suffix name of the uploaded file_javascript skills

WBOY
Release: 2016-05-16 16:59:36
Original
1290 people have browsed it
Copy code The code is as follows:

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
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template