Monday 5 May 2014

File Upload Validation with JavaScript Extension

 User can not Upload .jsp , .php Extensions type file.




<html>
<head>    
<script type="text/javascript">

function ChkfileExt(){
var id = document.getElementById('FileUpload2');
var id1 = document.getElementById("FileUpload2").value;

var img = id.value;
var n = img.match('.jsp');
//alert(n);
if (n == '.jsp') 
{
alert("Not Authorised , Only pdf, txt, csv, doc, docx, xls, xlsx ,jpg files can be uploaded");
return false;
}

var n = img.match('.php');
//alert(n);
if (n == '.php') 
{
alert("Not Authorised , Only pdf ,csv, doc, docx, xlsx ,jpg files can be uploaded");
return false;
}


var fileName =id.value;

if( id1=== "" ) {
//alert("Please upload any file.");
}
else{
if( fileName.lastIndexOf(".jpg")==-1 && fileName.lastIndexOf(".JPG")==-1 && fileName.lastIndexOf(".pdf")==-1 && fileName.lastIndexOf(".txt")==-1 && fileName.lastIndexOf(".csv")==-1 && fileName.lastIndexOf(".doc")==-1 && fileName.lastIndexOf(".docx")==-1 && fileName.lastIndexOf(".xls")==-1 && fileName.lastIndexOf(".xlsx")==-1){
alert("Only pdf, txt, csv, doc, docx, xls, xlsx ,jpg files can be uploaded");
return false;
}
}

}
</script>
</head>
<form id="form1" name="AddBillStatus" action="act.html" METHOD="POST" ENCTYPE="multipart/form-data"  >
<table align="center" class="sample" width="50%">

<br/><br/><br/><br/>
<tr>
<td nowrap="nowrap"> <b>Upload File</b></td>
<td ><input type="file" name="FileUpload2" id="FileUpload2" onChange="javascript:return ChkfileExt();" /> </td>
</tr>             
<tr>
<td>&nbsp;</td>
<td >
<input type="submit" name="btnAdd" value="Save" id="btnAdd" onClick="javascript:return ChkfileExt();" />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
<input id="Button2"  type="reset" value="reset"  />&nbsp; &nbsp;
</td>
</tr>
</table>
</form>
</html>

1 comment: