3

I just want ask if how I will block special characters such as <,>,",/,etc. in html input field?

Rob
  • 14,746
  • 28
  • 47
  • 65
Sean Cortez
  • 127
  • 2
  • 2
  • 7

3 Answers3

6

Why not use html5?

<input type="text" pattern="[^()/><\][\\\x22,;|]+">
RaShe
  • 1,851
  • 3
  • 28
  • 42
4

You can explicitly state which characters you accept HTML input pattern Attribute

If you insist on blocking specific characters you can use the following:

document.getElementById("explicit-block-txt").onkeypress = function(e) {
    var chr = String.fromCharCode(e.which);
    if ("></\"".indexOf(chr) >= 0)
        return false;
};
<input type='text' id='explicit-block-txt' value='' onpaste="return false"/>
Dror Moyal
  • 398
  • 3
  • 11
4

You can use regex.

document.getElementById("input").onkeypress = function(e) {
    /^[a-zA-Z0-9]+$/.test(this.value) // return true or false
};
<input type="text" id="input">
sametcodes
  • 583
  • 5
  • 8