2

I am new in javascript I want to create textbox which not allows blank space, I don't want to allow paste blank space also. Using jquery I can do it simply but I want to do it using javascript my jquery code is below,

$("input#UserName").on({
  keydown: function(e) {
    if (e.which === 32)
      return false;
  },
  change: function() {
    this.value = this.value.replace(/\s/g, "");
  }
});

I want to do the same thing using javascript help me, please ....

1
  • Check my solution! Commented Oct 18, 2016 at 10:58

4 Answers 4

3

You can use addEventListener to add listener. You can also use onInput to merge both events.

var text = document.getElementById('txtText');
text.addEventListener('input', function(e){
  var keyCode = e.keyCode ? e.keyCode : e.which;
  this.value = this.value.replace(/\s/g, '')
  if(keyCode === 32) return;
})
<input type='text' id="txtText">

Sign up to request clarification or add additional context in comments.

7 Comments

sir when I paste anything in this textbox then it paste space also
sir when I click on run code snippet and paste anything which considered space it paste space also
My bad. Test now.
sir is there any way to replace blank space with dash(-)
what do you mean by Blank space? entire input is blank then value should be -?
|
0

Try this,

HTML :

<input type="text" value="" maxlength="30" name="email" id="UserName">

Javascript :

document.getElementById("UserName").onkeypress = function(e) {
if (e.which == 32) {
return false;
}
}

Demo : Click here

1 Comment

when I paste anything then it allow for blank space
0

Here the transcription of your jQuery code

var input = document.getElementById("UserName");
if (input) {
  input.addEventListener('keydown', function(e){
    var key = e.which || e.keyCode;
    if (key === 32) {
      e.preventDefault();
      return false;
    }
  });
  input.addEventListener('change', function(){
    this.value = this.value.replace(/\s/g, "");
  });
}
<input type="text" id="UserName">

3 Comments

I don't have any space in my input box :/. What exactly is not working ?
sir when I paste anything in this textbox then it paste space also
When you leave the input, the space is removed
0

Please use below code to detect space

<input type="text" name="test" id="test">

document.getElementById("test").onkeypress = function (evt) {
        var k = evt ? evt.which : window.event.keyCode;
        if (k == 32) {
            return false;
        }
    }

To detected pest event. Please check below code. It will remove white space onblur event

document.getElementById("test").onblur = function (evt) {
       var x = document.getElementById("test");
       x.value = x.value.replace(/\s/g, "");
}

3 Comments

thanks, bro but when I paste anything in the textbox then space is allowed
@SmartDeveloper properly check your textbox id!
I have checked it properly it's working when I type anything in textbox then it not allows space but when I paste anything which considers space then it paste space also

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.