When I click to add a button with an empty value, empty value added in the list. Here I have attached a screenshot. I need validation for an empty value. My code is not working.
Error Output: https://prnt.sc/pk0hvt
HTML:
<form
id="listadding-wrap"
@submit="checkForm"
action="#"
method="post"
>
<input type="text" class="form-control" v-model="newName">
<button class="addbtn" @click="addName">+</button>
<ul class="teamlist">
<li v-for="teamMember in teamMembers" v-text="teamMember"></li>
</ul>
<p v-if="errors.length">
<b>Please enter the value to add</b>
<ul>
<li v-for="error in errors">{{ error }}</li>
</ul>
</p>
</form>
Vue JS:
var teammember = new Vue({
el: '#listadding-wrap',
data: {
errors: [],
newName:'',
teamMembers: ['']
},
methods: {
checkForm: function (e) {
if (this.newName) {
return true;
}
this.errors = [];
if (!this.newName) {
this.errors.push('Name required.');
}
e.preventDefault();
},
addName() {
this.teamMembers.push(this.newName);
this.newName = '';
}
},
});