2

VIEW

<div v-for="(listings, index) in list4" :key="index">
   <input v-model="listings.rfidState2" type="text"/>
</div>

<div v-for="(element2, index) in list4" :key="index">
  <p v-if="list4[index].rfidState2 > 0">WORKING</p>
</div>

If I insert value as AC87SG67A for an input field it throws me an error at v-if="list4[index].rfidState2 > 0" but if I insert the value as 98292001 it displays WORKING. Is there a way to display WORKING for any value inserted such as integer or alphabet(a to z) inside <input v-model="listings.rfidState2" type="text"/> textfield ?

3
  • 2
    v-if="list4[index].rfidState2.length > 0 ? Commented Jun 23, 2020 at 4:56
  • I guess it throws there is no definition if this literal and not a number is larger than any number? Commented Jun 23, 2020 at 5:14
  • 'AC87SG67A' is alpha numeric while 98292001 that is why you can compare it against 0 Commented Jun 23, 2020 at 5:36

2 Answers 2

3

First, you can add a .trim modifier to the input like:

<input v-model.trim="listings.rfidState2" type="text"/>

Now, any whitespace from user input to be trimmed automatically. Next, we can simply check if any text was entered or not like:

<p v-if="list4[index].rfidState2.length">WORKING</p>

So, if any integer or alphabet is entered then length will return a value greater than 0, which is truthy and thus the v-if will show the element, else it will be hidden.

Sign up to request clarification or add additional context in comments.

Comments

0

list4[index].rfidState2 is a string. You cannot compare it with a number. If you do so, JavaScript will do the implicit type conversion, and the result will not make sense. list4[index].rfidState2.length is the actual length of the string. So, what you should write is "list4[index].rfidState2.length > 0"

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.