0

There are two buttons called ADD and REMOVE. If the user clicks on ADD it will add one more input field for FULL NAME. I am using validationText to display text as PLEASE ENTER MORE THAN 5 CHARACTERS for full name. If I ADD two fields and insert only two characters in second one then it displays validationText on both input fields as

enter image description here

Is there a way to display validationText message to the particular field which consists of 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/

1 Answer 1

1

You only have one validationText for all fields. So, if you set it for one field, it's going to show up in the other field too.

I recommend doing something like this instead to show the validation:

<p v-if="minordatabase.full_name.length > 2 && minordatabase.full_name.length < 5" style="color: red;">
  Please enter more than 5 characters
</p>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.