Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to prevent using space while typing?

i have one textfield for input some serial number code.i want set this code show alert if someone use spase. it means space is not allowed and just allowed use minus for separate this code. Are you have any idea for resolve this problem? can i use jquery validate?

the correct typing:
135x0001-135x0100
like image 462
klox Avatar asked Sep 02 '10 02:09

klox


2 Answers

To prevent a space in your input element, you could do this using 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;
});​
like image 101
user113716 Avatar answered Sep 19 '22 14:09

user113716


Short and sweet NOT jQuery dependent

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

The HTML

<input type="text" name ="textbox" id="textbox" onkeyup="nospaces(this)">
like image 27
Mark Lalor Avatar answered Sep 20 '22 14:09

Mark Lalor