My goal is for the user to only enter number from [0-9] not even decimal is allowed
How to do that?
The code
<b-input expanded
type="number"
v-model="amount"
@input="updateValue()"
placeholder="{{ amount }}">
</b-input>
The <b-input>
is from buefy https://buefy.github.io/documentation/input/
By default, HTML 5 input field has attribute type=”number” that is used to get input in numeric format. Now forcing input field type=”text” to accept numeric values only by using Javascript or jQuery. You can also set type=”tel” attribute in the input field that will popup numeric keyboard on mobile devices.
1. Using <input type="number"> The standard solution to restrict a user to enter only numeric values is to use <input> elements of type number. It has built-in validation to reject non-numerical values.
With text input controls, there's no native way to restrict data entry to whole numbers only. This post describes a technique that we can use to limit input values to integers or whole numbers only. When building datra entry forms and screens, a common requirement is to configure text input controls to accept numeric input values only.
From the Beufy doc, I get that <b-input> is essentially an extension of native <input> field so it accepts attribute that the native input would accept. As of now, it is not possible to completely filter out specific characters using pure HTML attributes like pattern="\d+".
If you want user to restrict to only enter decimal points, as a workaround you can follow the below mentioned steps: 1) Keep the Text Format to Text. Set (resetvar,false);Set (resetvar,If (IsMatch (TextInput1.Text,"^ [1-9]\d* (\.\d+)?$"),false,true)) This will reset the control if user has given value that is not a decimal. Hope this Helps!
From the Beufy doc, I get that <b-input>
is essentially an extension of native <input>
field so it accepts attribute that the native input would accept.
As of now, it is not possible to completely filter out specific characters using pure HTML attributes like pattern="\d+"
.
What you can do is to use a "keydown" event listener to filter out these characters using native event.preventDefault()
by respective keys. Of course, we could use the native <input type="number">
to help in general.
const app = new Vue({
el: '#app',
methods: {
filterKey(e){
const key = e.key;
// If is '.' key, stop it
if (key === '.')
return e.preventDefault();
// OPTIONAL
// If is 'e' key, stop it
if (key === 'e')
return e.preventDefault();
},
// This can also prevent copy + paste invalid character
filterInput(e){
e.target.value = e.target.value.replace(/[^0-9]+/g, '');
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<input
type="number"
step="1"
min="0"
@keydown="filterKey"
// OR
@input="filterInput"
>
</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