Javascript validation for File uploading format

By | January 10, 2013

Upload file can be validate through javascript . By onclick, onchange, onsubmit we can do this validation. File extension can be analyzed  through below coding. You can change the file extension basis on your  requirement.Validation also for submit without any file selection.This javascript validation is simple and useful .

Javascript

[code type=html]

<script type=”text/javascript”>

function file_upload()
{
var imgpath = document.getElementById(‘word’).value;
if(imgpath == “”)
{
alert(“upload your word file”);
document.file.word.focus();
return false;
}
else
{
// code to get File Extension..
var arr1 = new Array;
arr1 = imgpath.split(“\\”);
var len = arr1.length;
var img1 = arr1[len-1];
var filext = img1.substring(img1.lastIndexOf(“.”)+1);
// Checking Extension
if(filext == “doc” || filext == “docx”)
{
alert(“File has been upload correctly”)
return false;
}
else
{
alert(“Invalid File Format Selected”);
document.form.word.focus();
return false;
}
}
}
</script>

[/code]

fileupload.php

Untitled

[code type=html]

<form name=”fileupload” onsubmit=”return file_upload();”>
<table align=”center” bgcolor=”#8080FF” height=”100″>
<tr>
<th> Fileupload:</th>
<td> <input type=”file” name=”word” id=”word” > </td>
</tr>
<tr>
<td> <input type=”submit” /> </td>
</tr>
</table>
</form>

[/code]

This alert message for invalid file format

1

This alert message will come when u choose correct file format.

2

 

 

 

4 thoughts on “Javascript validation for File uploading format

Leave a Reply to changdeo mhaske Cancel reply

Your email address will not be published. Required fields are marked *

intermittently-ruddily