Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Apr 2003
    Location
    Seoul, Korea
    Posts
    329
    Thanks
    0
    Thanked 0 Times in 0 Posts

    not to allow entering any characters

    Code:
    <form action='upload.php'>
    
    <input type='file' name='img'>
     
    <input type='submit'>
    
    </form>
    With the above code, a user can choose one of his or her file to upload.

    But a user can also enter some characters into the input box.

    I like to make the box not to allow entering any chracters but to allow only choosing one of an user's file to upload.

    Can I do it with your help?
    Get my greedy up

  • #2
    Regular Coder
    Join Date
    May 2005
    Location
    Michigan, USA
    Posts
    566
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <form action='upload.php'>
    	<input type='file' onkeydown='return false;' name='img'>
    	<input type='submit'>
    </form>
    Note: I do not test code. I just write it off the top of my head. There might be bugs in it! But if any thing I gave you the overall theory of what you need to accomplish. Also there are plenty of other ways to accomplish this same thing. I just gave one example of it. Other ways might be faster and more efficient.

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by SpirtOfGrandeur
    Code:
    <form action='upload.php'>
    	<input type='file' onkeydown='return false;' name='img'>
    	<input type='submit'>
    </form>
    No don't do that - that might prevent tabbing away from the input as well, breaking keyboard navigation.

    I suggest you don't do anything - validate the input on the server-side, with appropriate conditions for invalid filenames or files that don't exist. You have to do that anyway, so you may as well just do that.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #4
    Regular Coder
    Join Date
    May 2005
    Location
    Michigan, USA
    Posts
    566
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <form action='upload.php'>
    	<input type='text' />
    	<input type='file' onkeypress='return false;' tabindex=-1 name='img' />
    	<a href='#'>Some Text</a>
    	<input type='text' /> 
    	<input type='submit' />
    </form>
    Tested IE6.0, Opera 8.51, FF 1.5

    Even though Opera does not believe the hyperlink should have a tab stop. But that is their doing not mine!
    Note: I do not test code. I just write it off the top of my head. There might be bugs in it! But if any thing I gave you the overall theory of what you need to accomplish. Also there are plenty of other ways to accomplish this same thing. I just gave one example of it. Other ways might be faster and more efficient.

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by SpirtOfGrandeur
    Code:
    <form action='upload.php'>
    	<input type='text' />
    	<input type='file' onkeypress='return false;' tabindex=-1 name='img' />
    	<a href='#'>Some Text</a>
    	<input type='text' /> 
    	<input type='submit' />
    </form>
    That's even worse - taking the input out of the tab order altogether. It may be okay in the 3 browser you tested, but I would not feel comfortable doing that without the concern that for some it may prevent keyboard access to the input entirely - the button as well as the input.

    Maybe I'm just being superstitious, but there's experience behind that - it's not necessary to do any of this, and it carries usability and accessibility risks.


    Quote Originally Posted by SpirtOfGrandeur
    Even though Opera does not believe the hyperlink should have a tab stop. But that is their doing not mine!
    Opera uses different keys for anchor navigation - it uses "A" and "Q" - Tabbing is just for form elements.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #6
    Regular Coder
    Join Date
    May 2005
    Location
    Michigan, USA
    Posts
    566
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by brothercake
    That's even worse - taking the input out of the tab order altogether. It may be okay in the 3 browser you tested, but I would not feel comfortable doing that without the concern that for some it may prevent keyboard access to the input entirely - the button as well as the input.
    It does block it completely in all but Opera. And for the record I have horrible experiences with this also. I was just answering the question.
    Last edited by SpirtOfGrandeur; 01-25-2006 at 06:42 PM.
    Note: I do not test code. I just write it off the top of my head. There might be bugs in it! But if any thing I gave you the overall theory of what you need to accomplish. Also there are plenty of other ways to accomplish this same thing. I just gave one example of it. Other ways might be faster and more efficient.

  • #7
    New Coder
    Join Date
    Dec 2005
    Posts
    88
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If it was me I would use the onchange method:

    onchange="javascript:if(this.value.length == 1){this.value='';}"

    Since the onchange will run with each input, it will not allow manual input. The user will have to either browse or copy and paste. If you don't want them to copy and paste then you could also do onkeyup and check to see if the key entered is CTRL. If it is then wait until the value isnt '' and then change the value to '' and alert that they must browse to add the file.

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by SpirtOfGrandeur
    It does block it completely in all but Opera. And for the record I have horrible experiences with this also. I was just answering the question.
    Okay that's fair enough, but if I may be so bold - your answer doesn't put that in context, you just present it like "here's a solution". If you knew that it would probably break in most browsers, you really should have qualified your answer in those terms.

    If your kid asked you how to build a fire, would you just light it and walk away? Or would you point out the dangers of fire and the need to be careful, at the same time as showing them how?
    Last edited by brothercake; 01-26-2006 at 10:24 PM.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #9
    New Coder
    Join Date
    Jan 2006
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    XSS vulnerabilities are big problems these days.

  • #10
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    Quote Originally Posted by konithomimo
    If it was me I would use the onchange method:

    onchange="javascript:if(this.value.length == 1){this.value='';}"

    Since the onchange will run with each input, it will not allow manual input. The user will have to either browse or copy and paste. If you don't want them to copy and paste then you could also do onkeyup and check to see if the key entered is CTRL. If it is then wait until the value isnt '' and then change the value to '' and alert that they must browse to add the file.
    I don't think onchange works for a text box. Onkeydown, onkeyup, and onkeypress all present the problems of a user who holds down a key and places multiple characters before the event triggers the function.

  • #11
    Regular Coder
    Join Date
    May 2005
    Location
    Michigan, USA
    Posts
    566
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by brothercake
    If your kid asked you how to build a fire, would you just light it and walk away? Or would you point out the dangers of fire and the need to be careful, at the same time as showing them how?
    Actually I am the wrong person to ask these moral questions. And I think it is mostly from my maturity. If a kid (such as my younger brother) asked me how to build a fire (which he has) I would show him where the gas was and the lighter, and say "Good Luck". Now if he in return asked for hints or experiences with it (which he did) I would answer his questions with my experiences.

    I often times get confused on this board. Are we here to answer questions or are we here to teach the correct ways that are not set in stone (this is in no way a knock against you, or any one else in the forum). You see here I place a short blurb about a quick and dirty answer to a questions, even though I know it is wrong. In another post I worked on, i wrote along detailed explination and I almost always get better responces from the short blurb answers. So that would make me feel as the general attitude of this board is a help me quick and help me now, for I do not care about you or this board after you answer my questions. (A bit of a rant I guess) And in this I an thankful that I did not allow myself to write up a huge post naming every thing that is wrong/could be wrong with what the developer wants to do. When I take the time to write up a huge explination on the right/wrong (or whatever for that matter) I hardly ever get posts after my post, or any that are intelligent any ways, and I feel as though I have wasted my time explaining something.

    Just a guy ranting...
    Note: I do not test code. I just write it off the top of my head. There might be bugs in it! But if any thing I gave you the overall theory of what you need to accomplish. Also there are plenty of other ways to accomplish this same thing. I just gave one example of it. Other ways might be faster and more efficient.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •