0

Hi i am trying to restrict '>' and '<' symbols from form input fields. I tried but not working for me. This is my code:

$('form input:text').keyup(function(e){
    var x = e.which;
    if(x==188 || x==190){
        e.preventDefault();
        return false;
    }
    return true;
});

I tried both keyup and keypress but not working.

vairakkani
  • 51
  • 1
  • 1
  • 7
  • 2
    http://stackoverflow.com/questions/16052592/javascript-prevent-default-for-keyup keyup is too late – epascarello Nov 08 '16 at 14:18
  • 2
    See also: http://stackoverflow.com/a/3182548/519413. Note that if you're trying to stop someone adding HTML to the input, then this is about the least secure way of doing it – Rory McCrossan Nov 08 '16 at 14:19
  • You can also have an `input` event and there just replace value by `this.value = this.value.replace(/[^,.]/g, '')` – Rajesh Nov 08 '16 at 14:25
  • use `regex` [Regular Expression] – Jim Fahad Nov 08 '16 at 14:26
  • I don't see how this is a duplicate of "restrict special characters", which recommends a regex, if OP only wants to restrict two characters using their keyCodes. – Alexandru Severin Nov 08 '16 at 14:28
  • @vairakkani, If you really only need to block "<" and ">" and you don't want/need to use regex, just use keyCodes "60" and "62" instead of 188 and 190 (which are actually for comma and period). Also use `keypress` or `keydown` instead of `keyup` – Alexandru Severin Nov 08 '16 at 14:32
  • Thank you @Alexandru Severin – vairakkani Nov 08 '16 at 14:44
  • `code`$('form input:text').keypress(function(e){ var x = e.which; if(e.which == 60 || e.which == 62){ e.preventDefault(); return false; } return true; }); – vairakkani Nov 08 '16 at 14:46

0 Answers0