Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter input text to enter only number and not even decimal

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/

like image 258
sinusGob Avatar asked Dec 13 '18 03:12

sinusGob


People also ask

How do I make input text only accept numbers?

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.

How to restrict input to only numeric values in HTML?

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.

How to restrict data entry to whole numbers only?

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.

Is it possible to filter out specific characters in <B-input> field?

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+".

How to restrict text input to only decimal points?

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!


1 Answers

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>
like image 120
yqlim Avatar answered Oct 19 '22 16:10

yqlim