/    Sign up×
Community /Pin to ProfileBookmark

Checking if a file added to a (<input type=”file”>) is an image

Hi,

How can I check if a file added to a (<input type=”file” name=”add_image”>) form object is it really an image? I mean, for instance a ‘jpg’, ‘jpeg’ or ‘mpeg’ format.

And, Is it possible to erase the content of the (<input type=”file” name=”add_image”>) text field? I tried with ‘document.form_newOffer.add_image.value = “”;’ but it doesn’ t erase the content of the text field. Simply nothing happens.

The function that I am trying to write, will do this:

The user will click the ‘Examine’ button to add an image file, immediately after that, a little image sample of the image file added will be displayed below the form object. Then, if the user tries to add another file type, for instance a ‘.doc’ file, an alert will appear saying ‘you must add an image file’, the text field content (where the path is) will be erased, the sample image will be erased, and the previous cached image (show_Image.src) will be erased.

The code that I am trying to add is in bold, the rest of the code works fine without the part in bold:

[code]
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
<script language=”JavaScript”>
function showImg() {
[B]
if (document.form_newOffer.add_image.value = ‘an image file, jpg, jpeg or mpeg’) {[/B]

show_Image = new Image()
show_Image.src = document.form_newOffer.add_image.value
document.getElementById(“td_image”).innerHTML = “<img src='” + show_Image.src + “‘ name=’image_p’ width=’80’ height=’58’>”;
[B]
} else {
alert(“You must add an image file!”);
document.form_newOffer.add_image.value = “”; // I want to erase the text field content.
document.getElementById(“td_image”).innerHTML = “”; // I want to erase the sample image.

if (typeof show_Image != ‘undefined’) {
show_Image.src = “”; // I want to erase the previous image set in memory if it was set
}

}
[/B]

}
</script>

<title>Documento sin t&iacute;tulo</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>
</head>

<body>

<form name=”form_newOffer”>
<table width=”626″ height=”114″ border=”0″ cellspacing=”0″>
<tr>
<td width=”139″>Add a product image:</td>
<td width=”273″><input type=”file” name=”add_image” onChange=”return showImg();”> &nbsp;</td>
<td width=”208″>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td rowspan=”3″ id=”td_image”>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</form>

</body>
</html>
[/code]

Thank you

to post a comment
JavaScript

14 Comments(s)

Copy linkTweet thisAlerts:
@Warren86Sep 20.2004 — Erik:

I think this will help.

<HTML>

<Head>

<Script Language=JavaScript>

function showImg(){

tmpName = document.form_newOffer.add_image.value;
splitName = tmpName.split(".");
fileType = splitName[1];
if (fileType[1] == 'gif' || fileType == 'jpg' || fileType == 'jpeg')
{
document.form_newOffer.td_image.style.display = "";
document.form_newOffer.td_image.src = tmpName;
document.forms.form_newOffer.reset();
}
else{
alert("You must add an image file!");
document.forms.form_newOffer.reset();
}
}

</Script>

<title>Documento sin título</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

<Body>

<form name="form_newOffer">

<table width="626" height="114" border="0" cellspacing="0">

<tr>

<td width="139">Add a product image:</td>

<td width="273"><input type="file" name="add_image" onChange="showImg()"></td>

<td width="208"> </td>

</tr>

<tr>

<td> </td>

<td rowspan="3"><Img Src=null width='80' height='58' id='td_image'></td>

<td> </td>

</tr>

<tr>

<td></td>

<td> </td>

</tr>

<tr>

<td> </td>

<td> </td>

</tr>

</table>

</form>

<Script> document.form_newOffer.td_image.style.display = 'none' </Script>

</body>

</html>
Copy linkTweet thisAlerts:
@AdamGundrySep 20.2004 — Warren's example demonstrates how to check the extension of a file, but note that you cannot change the value of the file input control for security reasons.

Also note that you must repeat all these checks on the server.

Adam
Copy linkTweet thisAlerts:
@erick30authorSep 20.2004 — Hi Warren, that it's interesting, but it still doesn' t work. I suppose that the problem is here:

<i>
</i>tmpName = document.form_newOffer.add_image.value;
splitName = tmpName.split(".");
fileType = splitName[1];
if (fileType[1] == 'gif' || fileType == 'jpg' || fileType == 'jpeg')


If you check your code you will see that the alert always appears, I don' t know what is wrong..
Copy linkTweet thisAlerts:
@erick30authorSep 20.2004 — Adam,

I saw that 'document.form_newOffer.reset();' erase the text field content of the '<input type="file">', but as well all the 'form_newOffer' content.

You said that it wasn't possible to erase that text field, Why with this system it is possible?. Is it possible to erase only that text field? Not all the form content?
Copy linkTweet thisAlerts:
@erick30authorSep 20.2004 — Ok, the mistake was only here: (in bold)
<i>
</i>if (fileType[B][1][/B] == 'gif' || fileType == '[B]JPG[/B]' || fileType == 'JPEG') {


In my text field, the file path ends with 'JPG' instead of 'jpg'. A little bug.. ?
Copy linkTweet thisAlerts:
@erick30authorSep 20.2004 — Is it possible to check as well the file size? I mean, how many kilobytes occupy?
Copy linkTweet thisAlerts:
@JPnycSep 20.2004 — Yes, but I believe to do that you have to instantiate a windows scripting.fileSystemObject. 1 of it's methods is, I believe, getFileSize(), or something similar.
Copy linkTweet thisAlerts:
@Willy_DuittSep 20.2004 — A followup to Dunsel's last post:

<i>
</i>&lt;script type="text/javascript"&gt;
&lt;!--//
function getFile(file){
var fObj = new ActiveXObject("Scripting.FileSystemObject");
var form = document.forms['form'];

<i> </i>if(fObj.GetFile(file).Type.match(/image/gi)){
<i> </i> var image = new Image();
<i> </i> image.src = file;
<i> </i> form.size.value = fObj.GetFile(file).Size+' Bytes';
<i> </i> form.type.value = fObj.GetFile(file).Type;
<i> </i> form.width.value = image.width+' px';
<i> </i> form.height.value = image.height+' px';
<i> </i>}

<i> </i>else{
<i> </i> for(var i=0; i&lt;form.elements.length; i++){
<i> </i> form.elements[i].value = '';
<i> </i> } alert('Please choose an image!!');
<i> </i>}
}
//--&gt;
&lt;/script&gt;
&lt;/HEAD&gt;

&lt;BODY&gt;
&lt;b&gt;Select an Image File to check:&lt;/b&gt;
&lt;form name="form"&gt;
&lt;input type="file" name="file" onchange="getFile(this.value)"&gt;&lt;br&gt;
&lt;input type="text" name="size"&gt; : SIZE&lt;br&gt;
&lt;input type="text" name="type"&gt; : TYPE&lt;br&gt;
&lt;input type="text" name="width"&gt; : WIDTH&lt;br&gt;
&lt;input type="text" name="height"&gt; : HEIGHT&lt;br&gt;
&lt;/form&gt;
Copy linkTweet thisAlerts:
@erick30authorSep 20.2004 — thank you for your answers, now I have to go, I will check the forum later!
Copy linkTweet thisAlerts:
@Willy_DuittSep 20.2004 — FWIW: The below should check the file type and if not a file type ([i]included in the regular expression[/i]) it will erase the input type file value by removing it and replacing with another input of the same values ([i]name, type[/i])

<i>
</i>&lt;script type="text/javascript"&gt;
&lt;!--//
function chkExt(input){
if(!input.value.match(/[^/].(?:gif|jpg|png)/i)){
var newInput = document.createElement('input');
newInput.type = input.type;
newInput.name = input.name;
newInput.onchange = new Function('chkExt(this)');
input.form.replaceChild(newInput,input);
alert('Please choose an image file type!');
}
}
//--&gt;
&lt;/script&gt;
&lt;/HEAD&gt;

&lt;BODY&gt;
&lt;form&gt;
&lt;input type="file" name="image" onchange="chkExt(this)"&gt;
&lt;/form&gt;


.....Willy
Copy linkTweet thisAlerts:
@Warren86Sep 20.2004 — Erik:

No, I'm sorry, it was my error. Try it this way.

<HTML>

<Head>

<Script Language=JavaScript>

function showImg(){

tmpName = document.form_newOffer.add_image.value;

splitName = tmpName.split(".");

fileType = splitName[1];

if (fileType == 'gif' || fileType == 'jpg' || fileType == 'jpeg')

{

document.form_newOffer.td_image.style.display = "";

document.form_newOffer.td_image.src = tmpName;

document.forms.form_newOffer.reset();

}

else{

alert("You must add an image file!");

document.forms.form_newOffer.reset();

}

}

</Script>

<title>Documento sin título</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

<Body>

<form name="form_newOffer">

<table width="626" height="114" border="0" cellspacing="0">

<tr>

<td width="139">Add a product image:</td>

<td width="273"><input type="file" name="add_image" onChange="showImg()"></td>

<td width="208"> </td>

</tr>

<tr>

<td> </td>

<td rowspan="3"><Img Src=null width='80' height='58' id='td_image'></td>

<td> </td>

</tr>

<tr>

<td></td>

<td> </td>

</tr>

<tr>

<td> </td>

<td> </td>

</tr>

</table>

</form>

<Script> document.form_newOffer.td_image.style.display = 'none' </Script>

</body>

</html>
Copy linkTweet thisAlerts:
@erick30authorSep 20.2004 — Ok thanks,

Willy, your regular expression works fine.

Warren, now it is better, but I need 'JPG' and 'JPEG' in capital letters:
<i>
</i>if (fileType == '[B]gif[/B]' || fileType == '[B]JPG[/B]' || fileType == '[B]JPEG[/B]')
Copy linkTweet thisAlerts:
@Warren86Sep 20.2004 — Erik:

This changes the fileType to lower case, so that no matter which it is, the test for "gif", "jpg", and "jpeg" will always work.

<HTML>

<Head>

<Script Language=JavaScript>

function showImg(){

tmpName = document.form_newOffer.add_image.value;
splitName = tmpName.split(".");
fileType = splitName[1];
fileType = fileType.toLowerCase();
if (fileType == 'gif' || fileType == 'jpg' || fileType == 'jpeg')
{
document.form_newOffer.td_image.style.display = "";
document.form_newOffer.td_image.src = tmpName;
document.forms.form_newOffer.reset();
}
else {
alert("You must add an image file!");
document.forms.form_newOffer.reset();
}
}

</Script>

<title>Documento sin título</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

<Body>

<form name="form_newOffer">

<table width="626" height="114" border="0" cellspacing="0">

<tr>

<td width="139">Add a product image:</td>

<td width="273"><input type="file" name="add_image" onChange="showImg()"></td>

<td width="208"> </td>

</tr>

<tr>

<td> </td>

<td rowspan="3"><Img Src=null width='80' height='58' id='td_image'></td>

<td> </td>

</tr>

<tr>

<td></td>

<td> </td>

</tr>

<tr>

<td> </td>

<td> </td>

</tr>

</table>

</form>

<Script> document.form_newOffer.td_image.style.display = 'none' </Script>

</body>

</html>
Copy linkTweet thisAlerts:
@Willy_DuittSep 20.2004 — [i]Originally posted by erick30 [/i]

[B]Ok thanks,



Willy, your regular expression works fine.



Warren, now it is better, but I need 'JPG' and 'JPEG' in capital letters:

<i>
</i>if (fileType == '[B]gif[/B]' || fileType == '[B]JPG[/B]' || fileType == '[B]JPEG[/B]')
[/B][/QUOTE]


If you used the regular expression... there would be no need to worry about case since it includes the case insensitive flag [b]i[/b]....
×

Success!

Help @erick30 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...