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 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2002
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    prevent typing in textbox

    Hello list
    well this is i guess simple question ,i have <INPUT type="text" .....>
    now i like to prevent typing in some condition ,i have onkeydown=foo(window.event.keyCode)
    that checks the input that the user typed
    and this is the function
    ------------------------------------------------------------------
    function foo(x)
    {
    if(x=='32')
    return false;
    }

    ----------------------------------------------------------------------
    simple right ? but this is not preventing to type the char,
    do i need to remove the last char in the string every time ?
    something like :

    ---------------------------------------------------------------------
    var z=document.getElementById(y).value
    z=z.substr(0,z.length-1)
    document.getElementById(y).value=z;
    -----------------------------------------------------------------------
    why the return false; dont work in this case?


    thanks

  • #2
    Regular Coder
    Join Date
    May 2002
    Location
    Helsinki, Finland
    Posts
    231
    Thanks
    0
    Thanked 1 Time in 1 Post
    onkeydown reacts, when key is pressed, thus its acting after a letter is entered in textfield. If there were a property / event handler : onbeforekeypress it most probably would prevent entering text when returned false.
    Zvona
    First Aid for
    Web Design

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Make sure you code the call correctly:

    <input type="text" ... onkeydown="return foo(window.event.keyCode)">

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    paris, france
    Posts
    1,216
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if it is conditional dissabling you want then you could try this

    if (something = true)
    {
    document.thefrom.thetextbox.dissabled = true
    }
    else
    {
    document.theform.thetextbox.dissabled = false
    }
    photoshop too expensive? use the GIMP! www.gimp.org


  •  

    Posting Permissions

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