Vue js for filtering and replacing prohibited words

For this scenario, our objective is to screen the words in our input:

<input type="text" class="form-control" placeholder="Write something..." v-model="todoInput"">

Below are the restricted words that we aim to substitute in the input

"restricted", "orange", "pineapple",

Answer №1

Our Vue instance below demonstrates a watcher function that masks banned words with asterisks (*).

const app = new Vue({
    el: '#app',
    data: function(){
    return {
    todoInput : '',
    }
    },
    watch: {
    todoInput: function(){
    var banned = ["banned", "apple", "banana"]
    for (var i = 0; i < banned.length; i++) {
    if (this.todoInput.includes(banned[i])) {
    this.todoInput = this.todoInput.replace(banned[i], "*".repeat(banned[i].length)) //adjusts input to display * times the length of the banned word
    }
    }
    }
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.min.js"></script>

<div id="app">

    <input type="text" class="form-control"
            placeholder="Write something..." 
            v-model="todoInput">

</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Node.js failing to log chat messages

The goal is to log the chat message entered by the user in the console (terminal). Currently, there seems to be an issue with logging and I'm struggling to debug it. Keep in mind that I am a novice when it comes to NodeJS and only have basic knowledge ...

The dropdown in vue-multiselect automatically closes after the first selection is made, ensuring a smooth user experience. However,

I am experiencing an issue where the dropdown closes after the first selection, despite setting close-on-select="false". However, it works properly after the initial select. You can observe this behavior directly on the homepage at the following link: vue ...

JavaScript framework that is easily customizable to include support for XmlHttpRequest.onprogress, even if it needs to be emulated

Which JavaScript library or framework offers support for the "onprogress" event for XmlHttpRequest, even if it needs to be emulated using a plugin or extension? Alternatively, which JavaScript framework is the most straightforward to extend in order to add ...

Guide on retrieving the innerHTML of all <a> tags within a specific span class

<span class="mgen"> <a rel="tag">Action</a> <a rel="tag">Adventure</a> <a rel="tag">Apocalypse</a> <a rel="tag">Fantasy</a> <a rel="tag" ...

What are the best methods for preventing scss styles from leaking between pages?

I'm currently working on a project that includes the following files: /* styles/1.scss */ body { /* Some other styles not related to background-color */ } /* styles/2.scss */ body { background-color: blue; } // pages/one.js import "../styles/ ...

Dynamically import React Material UI Icons when needed

The concept here revolves around importing react material UI icons only when necessary, especially in situations where we may not know the icon name during compile time. (Ensuring that we have valid icon names) My method involved using a require statement ...

Creating Docker images in a lerna monorepo without the need for publishing

In the context of Lerna monorepos, the primary goal is to facilitate branch building and deployments. The challenge arises from the fact that Lerna monorepos either consolidate dependencies in NPM or utilize yarn workspaces to achieve the same outcome, re ...

Is it possible to disable the "super must be called before accessing this keyword" rule in babelify?

My current setup involves using babelify 7.2.0 with Gulp, but I've encountered an error when working with the following code snippet: class One {} class Two extends One { constructor() { this.name = 'John'; } } The issue at hand i ...

When an anchor link is dynamically created, the `click()` method does not activate the click event

After creating an anchor element using the document.createElement('a') method, I encountered an issue where the click event was not being triggered as expected. To provide more context, refer to the code snippet below: var link = document.create ...

Sending data between PHP and JavaScript using Ajax communication

I am currently working on a PHP page that involves variables and radio buttons. When a user clicks on a radio button, it triggers a JavaScript function to calculate the price of the selected item by passing some variables. Here's how I have implemente ...

VueDraggable: enabling cloning exclusively

Here is an example to illustrate the point: Let's say we have a shopping website where "Draggable 1" represents the items available for purchase and "Draggable 2" represents the user's shopping cart. Users should be able to drag items from ...

JavaScript event listener for SVG path element click is not functioning correctly as anticipated

How to determine if an element or its children have been clicked? I am trying to identify when a parent element or any of its child SVG icons with the attribute name set to "setGameState" have been clicked. The issue I am facing is that sometimes the even ...

Encountered issues while installing a package with npm instead of yarn

I have established a Git repository that will serve as an NPM package in another project. Let's refer to this sharable repository as genesis-service-broker. Within one of my services (specifically the activation service), I am utilizing this shareabl ...

What is the process for customizing a form in the "add event" feature of wdCalender?

I am looking to customize the form in wdCalendar to add events with fields like first name and last name instead of "what". I need to modify the quickadd function in jquery.calendar.js, which is triggered when a user clicks on the calendar to add an event. ...

What is the reason behind Intellij Code Inspection indicating that a selector is not being used?

I have a small project consisting of two files located in the same folder. App.css .App-align-left { text-align: left; } App.js import React from 'react'; import 'App.css'; class App extends React.Component { constructor(p ...

Testing Restful API Endpoints and Code Coverage with Node.js using Mocha

My time in Istanbul has been delightful, and I've been dabbling in different Node.js coverage libraries. However, I'm facing a challenge. Most of my unit tests involve making HTTP calls to my API, like this: it('should update the custom ...

Having trouble getting this.$set() to function properly with vue2.0 and Laravel integration

I'm currently attempting to store data in vue js and then display it on the view. This is my vue method: getVueItems: function(){ var vm = this; axios.get('/someuri').then(function(response) { vm.$set(this,'it ...

I possess an array containing objects of different lengths depending on the chosen city. How can I pinpoint the element that contains an object with a specific property?

My dilemma arises from the fact that the length of an array depends on the selected city, making it impossible to select elements using an index. In this scenario, I need to devise a method to choose elements based on the value of one of their properties. ...

Sorting two different divisions is an example

I need advice on how to toggle between two divs, A and B, without having to reload the page. Ideally, I would like to have three buttons - one that shows only div A when clicked, another that displays only div B, and a third button that shows both A and ...

Exploring the Challenges of Testing Vue Components with Slots Using Cypress

I am still learning about component testing with Cypress, and I have been exploring the documentation and examples to perform some basic component tests in my project. Recently, I encountered a situation where I needed to test a simple component that takes ...