Vue number input
by HemalathaThanigaivel
HTML
<div id="app">
<div>
<input type="number" v-model="price" @input="handleInput"/>
</div>
</div>
CSS
body {
padding: 2rem;
}
Vue
new Vue({
el: "#app",
data () {
return {
price: 12.34,
previousPrice: null
}
},
methods: {
handleInput (e) {
let stringValue = e.target.value.toString()
//let regex = /^\d*(\.\d{1,2})?$/
console.log(stringValue, 'jhj', this.price)
let regex = /^\d+(?:\.\d{1,2})?$/;
if(!stringValue.match(regex) && this.price!== '') {
console.log('if')
this.price= this.previousPrice;
// e.preventDefault();
}
this.previousPrice = this.price
}
}
})