ItSolutionStuff.com

HTML Tags are not Allowed in Textbox Validation using JQuery

By Hardik Savani • June 5, 2023
jQuery

I have two example that prevent HTML tags are not allowed in textbox using focusout event of jquery. You can chose any if you think it is best for me. In the first example i use regular expression for prevent html tags in my textarea and in second example i user directly check with html tags. So you can check both and use

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>

$(".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>

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>

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>

I hope it can help...

Tags: jQuery
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Calculate Number of Days Between Two Dates in JQuery

Read Now →

JQuery Remove Parent Table Row on Button Click Event

Read Now →

How to Check If Key Exists in JSON Object using JQuery?

Read Now →

PHP JQuery Ajax Post Request Example

Read Now →

How to Disable Browser Back Button using JQuery?

Read Now →

JQuery Rotate Image Animation Example Code

Read Now →

PHP JQuery Chosen Ajax Autocomplete Example

Read Now →

JQuery Reload Page After 5 Seconds Example

Read Now →

How to Get Gravatar Image from Email using JQuery?

Read Now →

How to Disable Right Click Menu using JQuery?

Read Now →

JQuery Notification Popup Box using Toastr JS Example

Read Now →

Check and Uncheck All Checkbox using JQuery Example

Read Now →

How to Check Object is Empty or Not in JQuery?

Read Now →