Prevent typing whitespace / disable spacebar in input - Javascript

How can I prevent typing of the whitespace character in input textbox by javascript function ?
26 Jan 2020 at 04:37 PM
0pnshow more
StorStor
To prevent typing whitespace in input field or textarea by using javascript, You can use this solution:


<script type="text/javascript">

function keyPressed(){
var key = event.keyCode || event.charCode || event.which ;
return key;
}

</script>

<input type="text" onKeyDown="javascript: var keycode = keyPressed(event); if(keycode==32){ return false; }" />



When a key is pressed, the function will get the keycode. The condition will check the keycode and if the spacebar is pressed it will return false. This will "disable" spacebar in input field or textarea.
01 Feb 2020 at 03:28 PM
0pnshow more
TimTim
Share on FacebookShare on TwitterShare on LinkedInSend email
2021 AnswerTabsTermsContact us