2013-08-25 53 views
0

可以請你告訴我如何捕捉文本區域中的空間。我阻止所有特殊字符。但我需要如果用戶單擊添加空間按鈕它不顯示警報但它包裝文本意味着不允許空間。有沒有什麼辦法可以在jQuery中捕捉空間

$(document).on("keyup", ".caseName_h",function() { 
    alert("hh"+$(this).val().contains(" ")); 
    if($(this).val()==" "){ 
    alert("hh"); 
    } 
if(/[^\w]/g.test($(this).val())) { 

    $(this).val($(this).val().replace(/[^\w]/g, "")); 

    PG_alert('Special characters not allowed!'); 

} 
}); 
+0

哪裏是添加空格鍵來自 –

+0

人會如何知道按鈕是否按下 –

+0

我想檢查文本框。 – Rohit

回答

0

總有辦法。試試這個

$('.caseName_h').keyup(function(e){ 
    $(this).val($(this).val.replace(/\s/g,'')); 
    if(e.keyCode == 32){ 
     PG_alert('Special characters not allowed!'); 
    } 
}); 
0

你可以像下面這樣做

$(document).on("keyup", ".caseName_h", function (e) { 
    //IE does not pass event 
    e = e|| window.event; 
    //32 is space key code 
    var keyCOde = e.keyCode ? e.keyCode : e.charCode 
    if (keyCode == 32) { 
     //DO stuff here 
    } 
}); 
0

您可以通過如果按空間使用的indexOf命令

var str = $(this).val(); if(str.indexof(" ") >=0){alert('spaces');} 
0

此代碼檢查檢查你的空間串

$(document).ready(function(){ 
$('#txtChar').keydown(function(e){ 
if (e.keyCode == 32) { 
alert('space') 
} 
}); 
}); 

jsFiddle

相關問題