PDA

View Full Version : prevent typing in textbox



umen
Jun 25th, 2002, 10:49 AM
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

Zvona
Jun 25th, 2002, 11:42 AM
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.

Roy Sinclair
Jun 25th, 2002, 06:13 PM
Make sure you code the call correctly:

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

whackaxe
Jun 25th, 2002, 06:17 PM
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
}