Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
173
rated 0 times [  174] [ 1]  / answers: 1 / hits: 44948  / 5 Years ago, wed, may 8, 2019, 12:00:00

My goal is to limit the number of digit user can enter in the input field. So a user can only input 10 digits. I tried min and max still doesn't work



Here's the code



        <input
v-model=amount
type=number
>


<script>
export default {
data() {
return {
amount: 7800
}
}

}

</script>


Right now i can add more than 10 digits


More From » vue.js

 Answers
16

We can control the value of <input> manually:



 <input
type=number
:value=amount
@input=updateValue
/>


and check in updateValue method:



  data() {
return {
amount: 7800
}
},
methods: {
updateValue(event) {
const value = event.target.value
console.log(value, this.amount)
if (String(value).length <= 10) {
this.amount = value
}
this.$forceUpdate()
}
}


Note that this.$forceUpdate() is used to make component re-render when user input more than 10 characters.



Demo on Codepen


[#52146] Tuesday, April 30, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
chase

Total Points: 78
Total Questions: 106
Total Answers: 93

Location: Comoros
Member since Tue, Mar 14, 2023
1 Year ago
chase questions
Thu, Mar 31, 22, 00:00, 2 Years ago
Thu, Jul 1, 21, 00:00, 3 Years ago
Sat, Dec 12, 20, 00:00, 4 Years ago
Mon, Sep 14, 20, 00:00, 4 Years ago
;