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 7 of 7
  1. #1
    Regular Coder
    Join Date
    May 2004
    Posts
    391
    Thanks
    76
    Thanked 0 Times in 0 Posts

    read only text field?

    Hi there. Is it possible to allow a user to only delete what is in a text field and not enter items into it??

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Do you mean delete ALL the content of the field, or just part of it?

    If the former you could do something like

    input type = "text" name = "field1" value = "Something" onclick="scrub(this)"

    function scrub(which) {
    which.value = "";
    }

    You could prevent the content being added to by checking the length and if greater than the original (or revised) length alert a message and return false.

    <SCRIPT type = "Text/Javascript">
    var maxlen = 9 // length of original text e.g. 'Something'
    function checklen (which) {
    newlen = which.value.length;
    if newlen > maxlen {
    alert ("You may only delete text from this field, not add more text to it")
    return false;
    }
    else {maxlen = newlen} // maxlen can reduce but not increase.
    }
    </SCRIPT>

    Note that this does not prevent the user from replacing the original text with some other text of equal length.
    Last edited by Philip M; 05-10-2007 at 07:45 PM.

  • #3
    Regular Coder
    Join Date
    May 2007
    Posts
    118
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Nikko.. maybe you want something like this :

    <script>
    function KeyDownHandler(event)
    {
    if ((event.keyCode == 8) || (event.keyCode == 46))
    return true;
    else
    return false;
    }
    </script>
    <input value="testtadsasdasdext" type="text" onkeypress="return KeyDownHandler(event);">
    But it's not so good how it looks, because if javascript is disabled in browser you have a problem
    Last edited by smalldog; 05-11-2007 at 01:01 AM. Reason: better version
    Forum for webmasters and developers
    http://www.htmlfrenzy.com
    Are you an IT expert? Join our Reward system
    www.htmlfrenzy.com/reward-system.php

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    You can still paste to the text field with this
    Code:
    if ((event.keyCode == 8) || (event.keyCode == 46))

  • #5
    Regular Coder
    Join Date
    May 2007
    Posts
    118
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rwedge View Post
    You can still paste to the text field with this
    Code:
    if ((event.keyCode == 8) || (event.keyCode == 46))
    You can still disable javascript in the browser
    Forum for webmasters and developers
    http://www.htmlfrenzy.com
    Are you an IT expert? Join our Reward system
    www.htmlfrenzy.com/reward-system.php

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by smalldog View Post
    You can still disable javascript in the browser
    Well, that applies to any script. In that case all bets are off. Can be overcome with <NOSCRIPT> tags to redirect to another page.

    Why (event.keyCode == 46)? ASCII 46 is a period.
    Last edited by Philip M; 05-11-2007 at 07:47 AM.

  • #7
    Regular Coder
    Join Date
    May 2007
    Posts
    118
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Why (event.keyCode == 46)? ASCII 46 is a period.
    In Firefox is 46 delete key, it seems in IE is 46 a period
    Last edited by smalldog; 05-11-2007 at 08:23 AM.
    Forum for webmasters and developers
    http://www.htmlfrenzy.com
    Are you an IT expert? Join our Reward system
    www.htmlfrenzy.com/reward-system.php


  •  

    Posting Permissions

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