Home Javascript How to textbox validation HTML tags are not allowed by using Jquery ?

How to textbox validation HTML tags are not allowed by using Jquery ?

by Shahriar Sagor

Today now in this post i will show you How to textbox validation HTML tags are not allowed by using Jquery ? Here I have few step that can prevent HTML tags are not allowed in the textbox by using focusout event of jquery.Sometimes we need to validate that any user can not enter any html tags on textbox input field. So here i will give you two example. i hope i will help you.

Example 1:
<html lang="en">
<head>
    <title>Jquery - HTML Tag are not allowed in textarea</title>
    <script type="text/javascript" src="//cdn.jsdelivr.net/jquery/1/jquery.min.js"></script>
</head>
<body>
<div class="container text-center">
	<form>
		<textarea class="box"></textarea>
		<button>Submit</button>
	</form>
    <script>
        jQuery('.box').focusout(function(e){
            var message = jQuery('.box').val();
            if(/<(br|basefont|hr|input|source|frame|param|area|meta|!--|col|link|option|base|img|wbr|!DOCTYPE|a|abbr|acronym|address|applet|article|aside|audio|b|bdi|bdo|big|blockquote|body|button|canvas|caption|center|cite|code|colgroup|command|datalist|dd|del|details|dfn|dialog|dir|div|dl|dt|em|embed|fieldset|figcaption|figure|font|footer|form|frameset|head|header|hgroup|h1|h2|h3|h4|h5|h6|html|i|iframe|ins|kbd|keygen|label|legend|li|map|mark|menu|meter|nav|noframes|noscript|object|ol|optgroup|output|p|pre|progress|q|rp|rt|ruby|s|samp|script|section|select|small|span|strike|strong|style|sub|summary|sup|table|tbody|td|textarea|tfoot|th|thead|time|title|tr|track|tt|u|ul|var|video).*?>|<(video).*?<\/\2>/i.test(message) == true) {
                alert('HTML Tag are not allowed');
                e.preventDefault();
            }
        });
    </script>
</div>	
</body>
</html>
Example 2:
<html lang="en">
<head>
    <title>Jquery - HTML Tag are not allowed in textarea</title>
    <script type="text/javascript" src="//cdn.jsdelivr.net/jquery/1/jquery.min.js"></script>
</head>
<body>

<div class="container text-center">
 <form>
 <textarea class="box"></textarea>
 <button>Submit</button>
 </form>
    <script>
        $(".box").focusout( function(e) {
            var reg =/<(.|\n)*?>/g; 
            if (reg.test($('.box').val()) == true) {
                alert('HTML Tag are not allowed');
            }
            e.preventDefault();
        });
    </script>
</div> 

</body>
</html>

Read Also : Laravel 8 Mobile Number Verification Tutorial

Thanks for read. I hope it help you. For more you can follow us on facebook

close

You may also like