Javascript - Validate string variable from HTML Contents

by barkkathulla 2014-04-16 15:56:56

The below method is used to check whether the given input string having html contents.

function containHtml(msg){

var StrippedString = msg.replace(/(<([^>]+)>)/ig,"");
msg_length=msg.length;
strippedstring_length=StrippedString.length;
if(msg_length!=strippedstring_length)
{
return true;
}

var a = document.createElement('div');
a.innerHTML = msg;
var j=0;
for (var c = a.childNodes, i = c.length; i--; ) {
if (c[i].nodeType == 1){
j++;
}
}
if(j>0)
{
return true;
}
else
{
return false;
}
}



var html_exists=containHtml(msg);
if(html_exists){
alert ("HTML Contents are not allowed");
return false;
}
893
like
0
dislike
0
mail
flag

You must LOGIN to add comments