2016-03-23 31 views
0
$('#name').keypress(function(e) { 
    if(e.which < 97 || e.which > 122){ 
     e.preventDefault(); 
    } 
}); 

sadece alfabe sadece ama aynı zamanda o da çalışmak için boşluk izin vermemek izin veriyorsa içinde. Alan kodunun 32 olduğunu biliyorum, ancak bu senaryoyu nasıl eklemeliyim?birden koşul kodu Üstü deyimi

+0

'(e.which eğer! = 32 && (e.which <97 || e.which> 122) && e.which) ' – Rayon

cevap

1

değerini dışlamak için bir and maddesini kullanın 32

$('#name').keypress(function(e) { 
 
    snippet.log(e.which) 
 
    if ((e.which < 97 || e.which > 122) && e.which != 32 && e.which != 45) { 
 
    e.preventDefault(); 
 
    } 
 
});
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> 
 
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input id="name" />

+0

dash' ın anahtar kodu 189, ben e.whch! = 189 yapıyorum, herhangi bir fikre izin vermiyor mu? – Jennifer

+0

@Jennifer Sanırım bir '' '' whch'' içinde eksik '' '' '' '' '' '' '' '' 0'''' –

+0

sanırım bu dize için daha iyi olmazdı 'String.fromCharCode (e.which) === "-" '? – Jai