How to prevent the use of space when typing?

I have one text box for entering the serial number code. I need to install this code showing a warning if someone is using spase. this means that space is not allowed and minus is allowed for a separate code. Do you have an idea to solve this problem? can i use jquery validation?

the correct typing:
135x0001-135x0100
+3
source share
3 answers

To prevent a space in the input element, you can do this with jQuery:

Example: http://jsfiddle.net/AQxhT/

​$('input').keypress(function( e ) {
    if(e.which === 32) 
        return false;
})​​​​​;​

.

$('input').keypress(function( e ) {    
    if(!/[0-9a-zA-Z-]/.test(String.fromCharCode(e.which)))
        return false;
});​
+18
source

Short and clear NOT jQuery dependent

function nospaces(t){
  if(t.value.match(/\s/g)){
    t.value=t.value.replace(/\s/g,'');
  }
}

HTML

<input type="text" name ="textbox" id="textbox" onkeyup="nospaces(this)">
+13
source
$('.noSpace').keyup(function() {
 this.value = this.value.replace(/\s/g,'');
});

<input type="text" name ="textbox" class="noSpace"  />
0

Source: https://habr.com/ru/post/1762771/


All Articles