C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » JavaScript »

How to check file extension in java script


Posted Date: 05 Jul 2008      Posted By: sachin      Member Level: Silver     Points: 1   Responses: 6



Hi,
It's urgent
How To check extension of file in java script
in asp.net





Responses

Author: Anand    05 Jul 2008Member Level: BronzeRating: 2 out of 52 out of 5     Points: 0

Spliting is the only way.


Author: chandramohan    05 Jul 2008Member Level: GoldRating: 2 out of 52 out of 5     Points: 1

var valid_extensions = /(.jpg|.jpeg|.gif)$/i;
//
function CheckExtension(fld)
{
if (valid_extensions.test(fld.value)) return true;
alert('The selected file is of the wrong type.');
fld.select();
fld.focus();
return false;
}

Then, you can call it as follows:

<form onsubmit="return CheckExtension(this.elements['fileupload'])">
<fieldset>
<input type="file" name="fileupload" size="40"
onchange="return CheckExtension(this)">
</fieldset>
</form>



Author: Aravind PK    05 Jul 2008Member Level: GoldRating: 2 out of 52 out of 5     Points: 3


function fnCheckExt(fileName)
{
var strExtn = fileName.value.substr(fileName.value,fileName.value.lastIndexOf(".")+1);
alert("Your file is of extension : "+ strExtn);
}



Author: kunal badgujar    08 Jul 2008Member Level: GoldRating: 2 out of 52 out of 5     Points: 6

hi..
try following code..

<script language="JavaScript">
function checkExt(formObj)
{
var filename = formObj.fileToUpload.value;
var filelength = parseInt(filename.length) - 3;
var fileext = filename.substring(filelength,filelength + 3);
// Check file extenstion
if (fileext != "gif" && fileext != "jpg")
{
alert ("You can only upload gif or jpg images.");
formObj.fileToUpload.focus();
return false;
}
formObj.submit();
}



Author: Nithya    08 Jul 2008Member Level: GoldRating: 2 out of 52 out of 5     Points: 4

hi...
in a javascript function you can create like this means it will check the file extension...

if((document.form1.upload.value.lastIndexOf(”.jpg”)==-1) {
alert(”Please upload only .jpg extention file”);
return false;
}

Explain: lastIndexOf will return the index of the last occurrence of the specified search argument. If not found, -1 will return.



Author: sridhar    09 Jul 2008Member Level: SilverRating: 2 out of 52 out of 5     Points: 6

//********************
Function For ValidatingFileUploadImage *************************
function onSubmitForm()
{
var formDOMObj = document.getElementById('FileUpload1').value;
if (formDOMObj == "")
{
error="";
return error;
}
else
{
var strLocation =document.getElementById'FileUpload1').value;
var strFile = strLocation.split(".");
var arrFileExtension = new Array(2)
arrFileExtension[0] = "JPG";
arrFileExtension[1] = "GIF";
arrFileExtension[2] = "PNG";
var len = arrFileExtension.length;
var x=0;
for (x=0; x<len; x++)
{
if (strFile[strFile.length - 1].toUpperCase() == arrFileExtension[x].toUpperCase())
{
error ="";
return error;
}
}
error="The selected file format is not supported.Only (jpg,gif,png)fromats are supported \n";
return error;
}

}



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : How to call the vbscript function from javascript
Previous : Finding value of GridView on clientside.
Return to Discussion Forum
Post New Message
Category: JavaScript

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use