let inp = document.getElementById("in");
function hendler(e) {
if(e.keyCode === 32) {
inp.value = null;
inp.value.trim();
}
}
inp.addEventListener('keydown', hendler, false);
<input type="text" id="in" >
var input = document.getElementById('in');
input.addEventListener('keydown', function(event) {
if (event.keyCode === 32) {
this.value = this.value.trim();
}
}, false);
if (!String.prototype.trim) String.prototype.trim = function() {
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
};