A
A
asferot2019-01-24 13:51:39
Vue.js
asferot, 2019-01-24 13:51:39

How to prevent negative numbers from being entered?

How to prevent negative numbers from being entered? There is an input field, it displays the number of product positions, there are + and -, which increase or decrease this number. But you can also enter a number and it should not be negative.

span(@click="product.quantity = Math.max(0, product.quantity - 1)" class="minus") &#8722
input(v-model.number="product.quantity" style="display: inline-block")
span(@click="product.quantity++" class="plus") &#43

Answer the question

In order to leave comments, you need to log in

3 answer(s)
0
0xD34F, 2019-01-24
@asferot

@input="onInput($event, product)"

methods: {
  onInput(e, product) {
    product.quantity = Math.max(0, parseInt(e.target.value) || 0);
  },
},

or
watch: {
  products: {
    deep: true,
    handler() {
      this.products.forEach(n => n.quantity = Math.max(0, parseInt(n.quantity) || 0));
    },
  },
},

A
Antonio Solo, 2019-01-25
@solotony

and make input type = number? and put min and max?

A
anlamas, 2019-01-24
@anlamas

Computed properties and tracking

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question