Reputation: 57
I am using a syntax like this
if(e.keycode == 17 ....
e.preventDefault()
and works for all keys controls, alts, tabs and etc all but one the space which the keycode is 38 I want to prevent the user from typing whitespaces in a textbox
Upvotes: 0
Views: 2087
Reputation: 40970
You can prevent user to type white space like this
just return false
if keycode is 32
otherwise return true
$("input").on("keydown", function (e) {
return e.which !== 32;
});
Note: You have mentioned Key code 38 but this is for UP
arrow key not for Space
Here is the list of Key Code, but unfortunately author didn't include the space's Key code in this list :)
Upvotes: 2
Reputation: 1
you can use jquery to do so . please check my sample here . http://jsfiddle.net/SinPride/9P29p/
<input type="text" id="txt_name">
<script>
$('#txt_name').keypress(function( e ) {
if(e.which === 32)
return false;
});
</script>
Upvotes: 0