Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent user to write specific words on <textarea>

Tags:

javascript

I've been watching a tutorial from developphp guy from youtube. I want to avoid people to write bad words on my text area from my form.

But I want to prevent them from writting several words, not only one, this tutorial doesn't explain that and I'm trying to find a way without succees, can someone explain me?

this is the actual javascript

HTML

<textarea class="ta" id="ta" name="ta" onkeyup="clean('ta')" onkeydown="clean('ta')" placeholder="A good word for a good word"></textarea>

JAVASCRIPT

<script type="text/javascript">
        function clean(el){
var textfield = document.getElementById(el);
var regex = /fuck/gi;
if(textfield.value.search(regex) > -1) {
    textfield.value = textfield.value.replace(regex, "");
    }
}
</script>

Thanks for your time guys!

like image 370
Eugenio Avatar asked Mar 07 '23 07:03

Eugenio


1 Answers

You can build your regex with multiple | (pipe) separated words:

textfield.value = textfield.value.replace(/badword1|badword2|badwordn/gi , "" );
like image 63
stacker Avatar answered Mar 15 '23 23:03

stacker