how to prevent using space while typing?

klox picture klox · Sep 2, 2010 · Viewed 37.3k times · Source

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

Answer

user113716 picture user113716 · Sep 2, 2010

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;
});​