I try to implement a maxLength
for an input from bootstrap-vue
, but from what I read from their documentation they don't support max. If I remove type="number"
, it works , but is not a number anymore.
<b-form-input
size="sm"
v-model="register_volume_first"
type="number"
maxlength=4
placeholder="1902"
></b-form-input>
Try to use formatter
prop as follows:
<template>
<div>
<b-form-input size="sm" v-model="volume" type="number" :formatter="formatYear" placeholder="1902"></b-form-input>
<div class="mt-2">Value: {{ volume }}</div>
</div>
</template>
<script>
export default {
data() {
return {
volume: '4'
}
},
methods:{
formatYear(e){
return String(e).substring(0,4);
}
}
}
</script>
maxLength only refers for text types, i tried to do a test to see if min & max are supported and it seems so, please take a look:
new Vue({
el: '#app',
methods: {
onSubmit(evt) {
evt.preventDefault()
console.log(evt)
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.min.js"></script>
<div id="app">
<b-form @submit="onSubmit">
<b-form-input type="number" max="20" min="10"><min="10" required></b-form-input>
<b-button type="submit" variant="primary">Submit</b-button>
</b-form>
</div>
Keep in mind that the check for min & max is done when the form is submitted, with the required parameter of course.
EDIT : added form & submit to the snippet
EDIT 2: Without form submit
new Vue({
el: '#app',
methods: {
onSubmit(evt) {
evt.preventDefault()
console.log(evt)
}
},
data() {
return{
inputValue: 15
}
},
watch: {
inputValue(val){
if(val < 10)
this.inputValue = 10;
else if(val > 20)
this.inputValue = 20;
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.min.js"></script>
<div id="app">
<b-form-input v-model="inputValue" type="number" max="20" min="10" required></b-form-input>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With