Prevent typing whitespace / disable spacebar in input - Javascript

How can I prevent typing of the whitespace character in input textbox by javascript function ?
0
give a positive ratinggive a negative rating
26 Jan 2020 at 04:37 PM
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.
0
give a positive ratinggive a negative rating
01 Feb 2020 at 03:28 PM
Tim
Share on FacebookShare on TwitterShare on LinkedInSend email
x
x
2024 AnswerTabsTermsContact us