-1

I have a text area in a page. If user try to give some html tags as a input and submit then i need to throw an error.

How can i get this done using jquery/javascript?

Could anyone please help me with this ?

satheesh
  • 123
  • 3
  • 13
  • Why throw an error when you can escape the tags? https://stackoverflow.com/questions/6234773/can-i-escape-html-special-chars-in-javascript – dislick Jun 22 '18 at 14:24
  • Basically that is my requirement. I can let the user enter only the text. So when they input html tag i should throw an error message. – satheesh Jun 22 '18 at 14:25
  • It's better to perform a whitelist instead of a blacklist – Emeeus Jun 22 '18 at 14:30
  • Possible duplicate of [Check if a string is html or not](https://stackoverflow.com/questions/15458876/check-if-a-string-is-html-or-not) – aloisdg Jun 22 '18 at 14:31
  • read @dfsq answer [here](https://stackoverflow.com/a/15458968/1248177) – aloisdg Jun 22 '18 at 14:31
  • @aloisdg the reply given by dfsq is not validating – satheesh Jun 27 '18 at 13:21

1 Answers1

0

Reply given in the above comment is not considering below html tags.

<html>,<body> and <head>

So this is how i implemented the requirement and it is working fine.

var textboxValue = document.getElementById("textbox").value;
var nonHtmlValue = textboxValue.replace(/<[^>]+>/g, '');
if(textboxValue!=nonHtmlValue)
{
    alert("HTML is not allowed");
}
satheesh
  • 123
  • 3
  • 13