Implementing Inappropriate words Validation using Javascript.
💻 coding

Implementing Inappropriate words Validation using Javascript.

1 min read 131 words
1 min read
ShareWhatsAppPost on X
  • 1The article explains how to implement JavaScript validation to block inappropriate words in user messages.
  • 2A JavaScript function, badwords(), checks input text against a predefined array of bad words.
  • 3The Message() function alerts users if their input contains inappropriate words before submission.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"The article explains how to implement JavaScript validation to block inappropriate words in user messages."

Implementing Inappropriate words Validation using Javascript.

This post about how to block Inappropriate/bad words content with javascript validation. Using this script we can stop bad messages/comments updates.

badwords.js

Contains javascript code. If you want to add some more words in bad_words_array

var bad_words_array=new Array("badword-1","badword-2","badword-3");
function badwords(txt)
{
var alert_arr=new Array;
var alert_count=0;
var compare_text=txt;

for(var i=0; i<bad_words_array.length; i++)
{
for(var j=0; j<(compare_text.length); j++)
{
if(bad_words_array[i]==compare_text. 
substring(j,(j+bad_words_array[i].length)).toLowerCase())
{
alert_count++;
}
}
}
return alert_count;
}

index.html

Contains javascript and HTML code. The form calling Message() function.

<script type="text/javascript" src="badwords.js"></script>
<script type="text/javascript">
function Message()
{
var textbox_val=document.form.textbox.value;
if(textbox_val=="")
{
alert("Please enter a message");
return false;
}

bwords=badwords(textbox_val);
if(bwords>0)
{
alert("Your message contains inappropriate words.
Please clean up your message.");
document.form.textbox.focus();
return false;
}
}
</script>

<form action="thankyou.html" method="post"
onsubmit="return Message();" name="form">
<textarea name="textbox"></textarea>
<input type="submit" value=" Send "/>
</form>

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 25 September 2019 · 1 min read · 131 words

Part of AskGif Blog · coding

You might also like