Tuesday, September 9, 2008

javascript validation to select only text or doc files

<html>
<head>
<script language="javascript" type="text/javascript">
function FileUpload()
{
if((document.getElementById('fileupload').value).length == 0)
{
alert("Select a File");
document.getElementById('fileupload').select();
return false;
}
var file = document.getElementById('fileupload');

var fileName = file.value;

var ext = fileName.substring(fileName.lastIndexOf('.') + 1);

if(ext == "TXT" || ext == "txt" || ext == "DOC" || ext == "doc" || ext == "DOCX" || ext == "docx" ||ext == "PDF" || ext =="pdf" || ext =="RTF" || ext =="rtf" )
{
return true;
}
else
{
alert("Select Doc, Txt, Pdf or Rtf Files Only");

file.focus();

return false;

}
return true;
}

</script>
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="30" align="right" valign="top" nowrap>Select File: </td>
<td><input type="file" name="fileupload" id="fileupload" size="30"></td>
</tr>
<tr>
<td align="center">
<input type="submit" name="upload" value="Upload File For Preview" onclick="if(!FileUpload()) return false;">
</td>
</tr>
</table>
</body>
</html>

No comments: