Js determines the file type and controls the form submission sample code

  • 2020-03-29 23:47:05
  • OfStack

The following code implements the control form submission
 
function shangchuan(){ 

var filepath = document.getElementById("file").value; 
alert(filepath); 
if(filepath==""){ 
alert(" Please choose the file to upload! "); 
document.getElementById("file").disabled=true; 

} 
var extname=filepath.substr(filepath.length-3, filepath.length); 
alert("extname::"+extname); 
//var extname = filepath.substring(filepath.lastIndexOf(".")+1,filepath.length); 
// extname = extname.toLowerCase();// Case handled  
if(extname!= "bmp" && extname!= "jpg" && extname!= "gif"){ 
alert(" Can only upload bmp,jpg,gif Format of the picture! "); 

document.getElementById("file").disabled=true; 
} 
else{ 
document.getElementById("file").disabled=false; 
} 

} 

 
<tr> 
<td><input name="file12" style="width:200px; height: 100px;" type="file" id="file" ></td> 
</tr> 
<tr> 
<td align="center"><button onclick="shangchuan()" > Click on the upload </button></td> 
</tr> 

Related articles: