Web Analytics Made Easy -
StatCounter prevent typing in textbox - CodingForum

Announcement

Collapse
No announcement yet.

prevent typing in textbox

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    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

    Comment


    • #3
      Make sure you code the call correctly:

      <input type="text" ... onkeydown="return foo(window.event.keyCode)">
      Check out the Forum Search. It's the short path to getting great results from this forum.

      Comment


      • #4
        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

        Comment

        Working...
        X