var field = document.querySelector('[name="username"]'); field.addEventListener('keypress', function ( event ) { var key = event.keyCode; if (key === 32) { event.preventDefault(); } });