Domanda

ho un campo di testo per inserire il codice del numero di serie. Voglio impostare questo codice per mostrare un avviso se qualcuno usa spase.significa che lo spazio non è consentito ed è consentito solo utilizzare il segno meno per separare questo codice.Hai qualche idea per risolvere questo problema?posso usare la convalida jquery?

the correct typing:
135x0001-135x0100
È stato utile?

Soluzione

Per evitare uno spazio nel tuo elemento di input, puoi farlo usando jQuery:

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

Altri suggerimenti

Corto e dolce non dipendente da jQuery

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

L'HTML

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

<input type="text" name ="textbox" class="noSpace"  />
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top