Vue showcases validation messages on a variety of form inputs

Two buttons labeled ADD and REMOVE are present. Clicking on ADD will generate an additional input field for FULL NAME. The validationText is used to prompt the user with "PLEASE ENTER MORE THAN 5 CHARACTERS" for the full name. If two fields are added and only two characters are inserted into the second one, the validationText is displayed in both input fields.

https://i.sstatic.net/IRfSC.png

Is there a way to show the validationText message only for the field with less than 5 characters?

View

<div id="app">
  <div class="work-experiences">

      <div class="form-row" v-for="(minordatabase, index) in minorsDetail" :key="index">

        <div class="col">
          <br>
          <label id="minorHeading">FULL NAME</label>
          <input v-model="minordatabase.full_name" type="text" class="form-control" placeholder="FULL NAME" size="lg" @input="checkValidation"/>
          <p v-show="!validationText" style="color:red;">
           Please enter than 5 characters
          </p>
        </div>

      </div>

    </div>

      <br>

      <div class="form-group">
        <button @click="addExperience" type="button" class="btn btn-info" style="margin-right:1.5%;">Add</button>
        <button @click="removeExperience" type="button" class="btn btn-outline-info">Remove Last Field</button>
      </div>
</div>

Script

new Vue({
  el: "#app",
  data: {
    minorsDetail: [
     {
       full_name: "",
       date_of_birth: "",
     }
    ],
    validationText: true
  },
  methods: {
    checkValidation(){
        console.log("SAN");
      var minorsDetailLastElement = this.minorsDetail[this.minorsDetail.length-1].full_name.length;
      console.log(minorsDetailLastElement);
        if(minorsDetailLastElement > 2){
        this.validationText = false;
      }
      if(minorsDetailLastElement > 5){
       this.validationText = true;
      }
    },

    addExperience(){
          this.minorsDetail.push({
            full_name: ''
          })

    },

    removeExperience: function(todo){

          var index = this.minorsDetail.indexOf(todo)
          this.minorsDetail.splice(index, 1)
          this.removeMinorFieldFunction();
    },

  }
})

Below is the code on JSFIDDLE

https://jsfiddle.net/ujjumaki/5mqp1bag/28/

Answer №1

Having a single validationText for all fields can cause issues where the validation message appears in multiple fields when set for one.

A better approach would be to customize the validation display as shown below:

<p v-if="minordatabase.full_name.length > 2 && minordatabase.full_name.length < 5" style="color: red;">
  Please input more than 5 characters
</p>

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

Identify the browser dimensions and implement CSS styling for all screen resolutions

I am currently facing an issue with a function that I have created to apply CSS changes to a menu based on browser resizing and different resolutions. The problem lies in the fact that my function does not seem to be correctly interpreted by the browser. W ...

How to align an image in the center of a circular flex container

I'm facing an issue in my Angular project where I have the following code snippet: onChange(event: any) { var reader = new FileReader(); reader.onload = (event: any) => { this.url = event.target.result; }; reader.readAsData ...

Exploring ways to find a particular value within an array of objects

Struggling to make my code render the data upon searching for a specific value. I attempted using a for loop, but I keep encountering an 'unexpected token' error. Is there a more effective way to accomplish this task? While this may seem like a s ...

Ways to resolve an unhandled promise in audio issues

A scenario where a piece of JavaScript is used to manage multiple audio elements is demonstrated below. var audio = []; audio["blue"] = new Audio("piano-blue.wav.mp3"); audio["red"] = new Audio("piano-red.wav.mp3"); ...

Updating a behavior object array in Angular 5 by appending data to the end

After creating a service to share data across my entire application, I'm wondering if it's possible to append new data to an array within the userDataSource. Here is how the service looks: user.service userDataSource = BehaviorSubject<Array& ...

Is there a way for me to record the variable's name instead of its linked data?

Currently, I am developing a node.js program that monitors the prices of different currencies. While I can successfully retrieve the prices, I would like the program to also display the names of the currencies along with their prices. In the code snippet b ...

How to pass an item as a parameter to a computed property in Vue.js, and have it return a sorted child array within a

After spending some time searching on Google, I am still struggling to find a solution for this issue. I have a list of "Intents" that contain nested lists of "Entities" created using v-for loops. The Intents are already computed, but now I need to dynam ...

In JavaScript coding language, you can use this syntax to create an array and push the

When the variable foo is defined, why does the following code behave in a certain way? var array = [].push(foo); When executed, why does the output equal 1? Based on my testing, the variable array simply returns the length of the array. Therefore, if ...

javascript context scope

As I delve into the world of JavaScript, please bear with me as I navigate through defining multiple namespaces. Here's an example of how I'm doing it: var name_Class = function(){ this.variable_name_1 = {} this.method_name_1 = function() { ...

Can you explain the function of mdLiveAnnouncer in Angular Material and how it operates?

Could someone provide an explanation of $mdLiveAnnouncer using this code snippet? module.controller('AppCtrl', function($mdLiveAnnouncer) { // Making a basic announcement (Polite Mode) $mdLiveAnnouncer.announce('Hey Google'); // ...

What is the best way to add a color swatch image using Javascript?

I'm facing a challenge in Shopify where I need to assign corresponding background images to color swatches. Currently, my icons are set up with the correct links for each color, but they are missing their respective images, similar to this example. I ...

What is the process for setting a cookie in Next.js from a different backend server?

I have encountered an issue with my Node.js API built using Express.js. The cookie I set works perfectly fine on Postman, but for some reason, it is not functioning properly in Next.js. I set the cookie when a user logs in, but it is not appearing in the b ...

Utilize client-side script in nodejs to share module functionalities

I created a function within the user controller module to verify if a user is logged in on the site: exports.isLoggedIn = function(req, res, next) { if (req.user) { return true; } else { return false; } }; I'm unsure of h ...

Creating a responsive design for mobile apps in Ionic using CSS

I need help with making my Ionic app responsive across all mobile devices. The design on the page was created using CSS, but it is not displaying properly on every device. How can I ensure that it adapts to different screen sizes? <template> <Io ...

Concern with Isotope's masonry feature

I'm at my wit's end! The container div I'm dealing with is 1170px wide. My goal is to fit in 3 divs within this width. The first div is 275px wide, the second is 580px wide, and the third is also 275px wide. Altogether, these divs should ad ...

Issue: The program is unable to access the 'length' properties as it is undefined while working with vue-lottie

I'm attempting to implement a Lottie animation in my Vue project using vue-lottie. I have been following the example code provided in the repository, but upon running the code, I encountered the following error message in the console: https://i.sstat ...

Creating server-side rendering for a Vue.js application with the Django framework

I am looking to incorporate server rendering into a section of my application. Our project heavily relies on vuex and vue router. All data is fetched using API requests. If anyone could guide me in the right direction or offer a code snippet, I would gre ...

Using Javascript to enable drag and drop functionality

After attempting to use a guide for creating drag and drop features, I encountered an issue. I followed the steps outlined at Despite the detailed instructions in the guide, I cannot seem to get the functionality to work. Is there a mistake in my code? I ...

Issue with Angular 13 Bootstrap5 Navbar's functionality

Angular 13 is my framework of choice, and I recently integrated Bootstrap 5 into my project using the command below: ng add @ng-bootstrap/ng-bootstrap As for the index.js file content, it looks like this: <!doctype html> <html lang="en" ...

Is it possible to enhance an external class with a non-static method using prototypes?

Is it possible to use prototypes to add a function for a class instance? allowing me to access this or __proto__ keyword inside my method, like so: class PersonClass { name: string; constructor(name: string) { this.name = name; } sayHello() ...