1

guys I am using the following code which works fine on pc but for mobile google chrome browser users are able to use space bar which I have disabled.

<script language="javascript" type="text/javascript">
function check(e) {
    var keynum
    var keychar
    var numcheck
    if (window.event) {
        keynum = e.keyCode;
    }
    else if (e.which) {
        keynum = e.which;
    }
    keychar = String.fromCharCode(keynum);
    if (keychar == " " || keychar == "'" || keychar == "`" || keychar =="!" || keychar =="@" || keychar =="#" || keychar =="$" || keychar =="%" || keychar =="^" || keychar =="&" || keychar =="*" || keychar =="(" || keychar ==")" || keychar =="-" || keychar =="+" || keychar =="=" || keychar =="/" || keychar =="~" || keychar =="<" || keychar ==">" || keychar =="," || keychar ==";" || keychar ==":" || keychar =="|" || keychar =="?" || keychar =="{" || keychar =="}" || keychar =="[" || keychar =="]" || keychar =="¬" || keychar =="£" || keychar =='"' || keychar =="\\") {
        return false;
    } else {
        return true;
    }
}
</script>

other than _ and - I wish nothing for user to enter even copy paste should be disabled.

Marius
  • 15,148
  • 9
  • 56
  • 76

1 Answers1

0

possible duplicate of answer by Stephan I'm not sure how you're passing your event into your function but you can try using window.onkeydown and e.target on document.body as described.

Here is a snippet which might also help tidy your code?

var keylist = [ "_", "-"];

window.onkeydown = function(e) {
   keychar = String.fromCharCode(e.keyCode);
   if(keylist.indexOf(keychar)===-1 && e.target == document.body){
      e.preventDefault();
      return false;
   }
}
Community
  • 1
  • 1
Sam0
  • 1,459
  • 1
  • 11
  • 13
  • whats the usage i to use it on input filed? – johram pong Sep 19 '16 at 13:38
  • this snippet applies to the global space of the window. if you want to register keypresses when you're in the input field then: http://stackoverflow.com/questions/11365686/how-to-get-text-of-an-input-text-box-during-onkeypress – Sam0 Sep 19 '16 at 16:26