PDA

View Full Version : validation for <INPUT TYPE="FILE">



PradheepAyyanar
06-29-2006, 11:26 AM
yo yo

can we validate the <INPUT TYPE="FILE">?

WHILE UPLOADING A FILE I NEED TO RESTRICT THE USER FROM ENTERING TEXT DIRECTLY IN THE TEXT FIELD AND SOME FILE VALIDATION HAS TO BE DONE

help me out in handling

Vallim
06-29-2006, 12:15 PM
Hi Pradeeh,

You can restrict the user entering file name directly to the textbox by writing a Keypress event as

Script

<script language="javascript" >
function onkeyPress(e)
{
var key = window.event ? e.keyCode : e.which;
if (key == 13)
StartClick();
e.cancelBubble = true;
e.returnValue = false;
return false;
}
</script>

Html

<input id="File1" style="position: relative" type="file" onkeypress="return onkeyPress(event);">

Can u be still clear on the other validation to be done.

Regards,

Valli

(www.syncfusion.com)

ASP.Net FAQ:
http://www.syncfusion.com/faq/aspnet/default.aspx

Complete set of ASP.Net controls and components
http://www.syncfusion.com/toolsweb
Valli

Twey
06-29-2006, 02:57 PM
It ought to be done server-side. And what kind of validation are you talking about?

PHP FAQ:
http://www.php.net/FAQ.php

Complete set of PHP functions
http://www.php.net/manual/en/funcref.php

PradheepAyyanar
06-30-2006, 04:22 AM
valli

my requirement is that the use sudn't even enter a valida path. he has options only to select a file and on uploading [clicking the button] it has to check whether a file is been selected.


Twey

NO SERVER_SIDE VALIDATION...

PraDheep

djr33
06-30-2006, 04:53 AM
my requirement is that the use sudn't even enter a valida path.Whoa, huh?

Note that whatever you want for this validation, you'll still need something serverside to recieve the file, so... sure, add a user friendly thing that will stop them from wasting with, with javascript, but you should also add something more secure (javascript can be gotten around) serverside to check the file then as well.

Twey
06-30-2006, 12:12 PM
Twey

NO SERVER_SIDE VALIDATION...Then you're wasting your time. Why can you not use server-side validation? As djr33 says, you must already be using a server-side script, or your form won't do anything :)
he has options only to select a file and on uploading [clicking the button] it has to check whether a file is been selected.There's not really much you can do. The file input is a rather unstandardised element, and although it usually manifests itself as the popular textbox and button, it doesn't have to according to the standards. It offers virtually no control over itself.

djr33
06-30-2006, 07:13 PM
Plus, you won't have access to the person's computer to check if the file does exist... so... yeah.


And, again, don't you want secure? The only way IS server side.

DimX
06-30-2006, 08:20 PM
Well, actually there is a "tricky" way to style file inputs.
Check out this: http://www.quirksmode.org/dom/inputfile.html

Twey
06-30-2006, 09:40 PM
Ah, clever :)

PradheepAyyanar
07-04-2006, 06:17 AM
Thanks all 4 posting and solving my issues and thanks for DimX who show the styles of input type file.

PraDz

vijay_jp2003@yahoo.com
02-20-2007, 07:30 AM
See if this solves,

function validateFile() {
event.returnValue=false;
}
..........
..........
..........
<input type="file" onKeyPress="return validateFile(this);" />

Twey
02-20-2007, 11:49 AM
Er, not really. It will always return false, since there's no way to actually validate the input. A function that always returns false isn't "validation" :)

Also, that will only work in IE, and will throw an error in other browsers, since event is undefined.

fernan_net
07-16-2008, 04:12 PM
Er, not really. It will always return false, since there's no way to actually validate the input. A function that always returns false isn't "validation" :)

Actually IMO that's the smartest solution. By always returning false to onKeyPress it will make it impossible to write text directly into the textbox, so the user needs to use the Browse button to select a file, this guarantees that the file exists (unless you delete it after selecting it but before submitting the form) so in a way it IS validation since it assures that the input is VALID.


Also, that will only work in IE, and will throw an error in other browsers, since event is undefined.

True, the given syntax does not work on all browsers but if you change "event.returnValue = false" to "return false" it should work on most browsers and with little effort this concept can be used with any browser.

...for those talking security, how does validating that the file exists on the server site improves security? I don't think this is at all a matter of security. It is rather a matter of performance, a postback consumes bandwidth and server resources so making sure that the request is valid before posting it to the server is a must. I guess that's why the threadstarter is asking for a client-side solution which I think makes a lot of sense.