Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vuejs set a variable maximum for a number input

I have the following vue js script:

 <template>
    <div>
        <div v-if='cart.length > 0'>
            <h1>Your Cart</h1>
        <template>
            <fieldset v-for='product in cart'>
                <image :src='product.image'
                <h4>{{product.name}}</h4>
                <input type='number' :max='quantCheck'/>
                <h5>{{product.price}}</h5>
            </fieldset>
        </template>
        </div>
        <div v-else><h1>Your Cart Is Empty</h1></div>
        <br />
        <h5>Subtotal: </h5>
        <h5>Shipping: Free for a limited time!</h5>
        <h2>Total: </h2>
    </div>
    </template>
    <script>
    const apiURL = 'http://localhost:3000';
    import axios from 'axios';
    export default {
        data() {
            return {
                cart: [
                        {
                    id:"56461",
                    name:"lilly",
                    quantity: 2,
                    price: 30.10
                    }, {
                    id:"1253",
                    name:"wild",
                    quantity: 1,
                    price: 31.10
                    }
                    ]
                }
            },
        methods: {
                let quantCheck = this.cart.product.quantity

        }
        }
    </script>

I haven't been able to find a good way to make something like this work.

The quantity is variable, and I guess maybe I could make a function that checks the number after each input and pops an error when it goes above but that's not quite the goal.

Anyway sorry if this is a stupid question but thanks for your help in advance!

like image 910
Trevor Jakeman Avatar asked Mar 14 '19 15:03

Trevor Jakeman


1 Answers

You can use HTML Form validation for input (type="number"):

<input type='number' :max='product.quantity'/>

If the input is greater than max value then it will show error on Submit the form

like image 66
Sajib Khan Avatar answered Oct 17 '22 03:10

Sajib Khan