I just want ask if how I will block special characters such as <,>,",/,etc. in html input field?
-
3By "block" do you mean not allowed to be typed in? What about pasting text?Spencer Wieczorek– Spencer Wieczorek2017-10-14 17:18:39 +00:00Commented Oct 14, 2017 at 17:18
-
For what purpose? Are you trying to prevent the user from using HTML?ceejayoz– ceejayoz2017-10-15 03:34:26 +00:00Commented Oct 15, 2017 at 3:34
Add a comment
|
3 Answers
Why not use html5?
<input type="text" pattern="[^()/><\][\\\x22,;|]+">
1 Comment
sametcodes
This way must to use in a form.
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">
1 Comment
vsync
It is not blocking.
pattern is for validation onlyYou 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"/>