View Full Version : how to validate the browse file button ???

02-07-2007, 12:44 PM
I've written the code as in jsp :
<html:file property="browsefile" value="" onkeyup="keyp()" />
and in the script i wrote as:
function keyp() {
alert("Please select the file through the browse button ! ");

what i want is , not allowing the user to input the file manually. he must have browse the file through the browse button and shouldn't write it manually.
Here in this i've to use the struts one html file option because i'm using this function for other.
Here what is happening is, when i press any key it is popping up the message but after click on OK, the value which i entered is remains there only eventhough i'm using :document.AddDeSelNumForm.browsefile.value='';
but the strange think is that when i'm alerting that particular value it is showing the the value of the browse text which u have entered the text. I'm really confused why it not clearing the field. Please help me out in this reagard or suggestions are always welcome.
Please suggest me how to do or please fix this bug.

in advance thanks a lot.....

02-07-2007, 02:20 PM
not entirly sure here but cant you just disable the field and the browse button still works?

02-07-2007, 07:01 PM
how to disable the text field of browse button so that browse button still works. I don't know how to disable such thing. Could anyone tell me how to achieve this problem.
Please help me out in this regard....
in advance thanks....

02-08-2007, 12:17 AM
I have one question, whats the difference between them clicking the button and manually typing in the path? Why do you need the person to click the browse button?

02-08-2007, 06:13 AM
Because i'm retreiving the path of the directive and doing the bulk insert in the database. Two operation has to be done one is Replace and one is Append. So, in this case if the user enter through manually then it might be the case that the user entered the wrong or some other path. So, i don't want the user to use the manually script.
There are also some other logic to do this, it is going to the action class(Struts) and there the manipulation is occuring . If the user input on the jsp page then it gets the javascript error. SO, i want to put the text field of the browse button disable and only through the button the user can access or some other logic is there then please forward me.....
I'm in confused state, please help me out.....
Also, tell me in my code where the logic overflows (PLEASE)....

02-08-2007, 07:53 AM
You can try using

inside of the file input. This in theory won't allow users to type in the box. It will move their cursor out of the file input. I'm not sure if this will work on the web but you could try it.

02-08-2007, 08:04 AM
I really don't know much about the subject but I read a post recently that said security for the <input type="file"> element is more strict than most inputs. I don't know if that applies to writing to the value or not.

Here is the post.

Don't add the quotes, do what Bill suggested. The reason its likely not working is because the file type input has a higher security than the other input boxes. I've tried this before. On a internet level the file input doesn't seem to allow you to get its value. Try it locally on your desktop of your computer it probably will work. Put some alerts into the function to see if its even being called.

I suspect the reason you can't change it is because you could change it to some file on the users machine and they could accidently upload a sensitive file.

Here is some code similar to yours that I wrote to text it. I had the same problem you had.

<script type="text/javascript">
function doIt(id) {
var o = document.getElementById(id);
var s = o.value;
o.value = "";

var adiv = document.getElementById(id + "div");
adiv.innerHTML = id + " = '" + s + "'";
Type something in each file then click the buttons to try and delete it<br/>
These two buttons use the same function<br />
<button onclick="doIt('text');">Get and Delete Text</button>
<input type="text" name="text" id="text" />
<button onclick="doIt('file');">Get and Delete File</button>
<input type="file" name="file" id="file" />
<div id="textdiv"> </div>
<div id="filediv"> </div>


02-08-2007, 08:43 AM
again i'm getting the problem. First thing is that when i'm using with :
onkeypress="this.blur()", it is working fine but when i copy and paste it is throwing error again. Can't it possible to disable the text field of browse button.
And Second thing is that i can't use the ids with the struts framework. I've written the code above , please have a look over it and please tell what should i do now.....
Any other thing or way , can i modelate my code...

02-08-2007, 12:37 PM
You can try using

inside of the file input. This in theory won't allow users to type in the box. It will move their cursor out of the file input. I'm not sure if this will work on the web but you could try it.

Have you tried the following

onkeypress="this.value = '';"

that would then null the field every time the key is pressed rather than loosing focus, what is the error message you are receiving? I am guessing its a security error one correct?