•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 374,023 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,784 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 3798 | Replies: 3
![]() |
•
•
Join Date: Jul 2006
Location: Deptford, London
Posts: 916
Reputation:
Rep Power: 5
Solved Threads: 46
Are you sure you don't mean 'file' fields?
As for checking the file; you should be able to interogate the value of said file field, split it by slashes, and then split it by .:
(this WONT validate a form; it will just show you how to get the file extension from a filespec in string format. post back if you have any problems integrating this with existing code...)
Tested on Firefox and Opera.
As for checking the size of the file; this certainly can't be done with JS. You have to write some server side code to handle the upload correctly. That is probably a better way to check the filetype aswell; by interogating the MIME type of an uploaded file rather than just assuming the extension indicates filetype.
input type="image" fields are just graphical submit buttons; input type="file" fields let a user pick a file on their filesystem to upload..As for checking the file; you should be able to interogate the value of said file field, split it by slashes, and then split it by .:
<html>
<head>
<script type="text/javascript">
function getFileType(sValue)
{
var aParts = sValue.split( "/" );
var iParts = aParts.length;
if( iParts >= 1 )
{
var sFile = aParts[ iParts - 1 ];
var aFile = sFile.split( "." );
if( aFile.length == 2 )
{
sName = aFile[0];
sExt = aFile[1];
alert("Filename is: "+sName+" extension is: "+sExt);
}
else
{
alert("Bad filespec; should have one dot between name and extension");
}
}
else
{
alert("No file selected");
}
}
</script>
</head>
<body>
<form>
<input type="file" id="file_input"/>
<span onclick="getFileType(document.getElementById('file_input').value);">GetFileType</span>
</form>
</body>
</html>(this WONT validate a form; it will just show you how to get the file extension from a filespec in string format. post back if you have any problems integrating this with existing code...)
Tested on Firefox and Opera.
As for checking the size of the file; this certainly can't be done with JS. You have to write some server side code to handle the upload correctly. That is probably a better way to check the filetype aswell; by interogating the MIME type of an uploaded file rather than just assuming the extension indicates filetype.
Last edited by MattEvans : May 29th, 2007 at 9:02 am.
If it only works in Internet Explorer; it doesn't work.
Just by validating the extension, you can't be sure that the file uploaded by the user is actually a image file. I can always rename an executable and make it seem like an image file, if your logic performs these kind of critical validations at the client side.
Better leave off these things to the server side validation. The size restriction as well as the type of file can only be meaningfully validated at the server. Doing it at the client side is a moot point.
Better leave off these things to the server side validation. The size restriction as well as the type of file can only be meaningfully validated at the server. Doing it at the client side is a moot point.
"I don't accept change. I don't deserve to live."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
- How to validate the textbox in datagrid using javascript? (ASP.NET)
- Javascript alert not working (JavaScript / DHTML / AJAX)
- calculate image property in javascript (JavaScript / DHTML / AJAX)
- case and image help please (PHP)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: How would I display somethign only if javascript is enabled?
- Next Thread: javascript function



Linear Mode