C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




How to check file extension in java script


Posted Date: 05 Jul 2008      Total Responses: 6

Posted By: sachin       Member Level: Silver     Points: 1



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





Responses

Author: Anand    05 Jul 2008Member Level: BronzeRating:     Points: 0

Spliting is the only way.


Author: chandramohan    05 Jul 2008Member Level: GoldRating:     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    05 Jul 2008Member Level: GoldRating:     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:     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:     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:     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
You must Sign In to post a response.
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   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use