views:

47

answers:

2

hello all.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
+3  A: 

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;
});​
patrick dw
klox
@klox - Not sure what you mean. Which characters to you want to allow, or which do you want to prevent?
patrick dw
klox
@klox - I assume you want to allow numbers and "x" as well. How about other letters?
patrick dw
@klox - I updated the answer. Now it only allows numbers "-" and "x". Was that what you wanted?
patrick dw
no..all alphabet is allow..and for the another marking which allow is just "-".may after this you can understand what i want.
klox
may be i'm just need your first answer.can you roll it back?
klox
A: 

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)">
Mark