0

can you please tell me how to catch space in textfield .I block all special character .But I need if user click add space button it don't show alert but it wrap the text mean not allowed space.

  $(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!');

}
});   
OptimusCrime
  • 14,662
  • 13
  • 58
  • 96
Rohit
  • 685
  • 3
  • 13
  • 28

4 Answers4

0

You can do it like this

$(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
    }
});
TheCodeDestroyer
  • 763
  • 9
  • 29
0

You can check your string on spaces by using the indexof command

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

This code checks if space is pressed

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

jsFiddle

Paja Aleksic
  • 181
  • 3
0

There are always ways. Try this

$('.caseName_h').keyup(function(e){
    $(this).val($(this).val.replace(/\s/g,''));
    if(e.keyCode == 32){
        PG_alert('Special characters not allowed!');
    }
});
commit
  • 4,777
  • 15
  • 43
  • 70