Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

223
Views
Rejecting non digits in vue input

Does somebody knows how to implement a VUE input field that rejects entering non digits as the user tries to enter it.

Some kind of:

<b-form-input v-on:keypress="onlyNumber(...."

Thanks!

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

The easiest way would be to declare the input type to number. <b-form-input type="number" ...

over 4 years ago · Santiago Trujillo Report

0

Finally, for me, this do the job:

    <input v-on:keydown="onlyNumber($event)" ... />

    

onlyNumber: function(evt) { //0035621
        evt = (evt) ? evt : window.event;
        var charCode = (evt.which) ? evt.which : evt.keyCode;
        if (charCode > 31 && (charCode != 46 &&(charCode < 48 || charCode > 57))) {
            evt.preventDefault();
        } else {
            return true;
        }
    }
over 4 years ago · Santiago Trujillo Report

0

You can use keyCode of the key pressed on keydown event and perevent default behavior if keyCode isn't for a non digit key;

Vue input component:

<b-form-input @keydown="onlyNumbers" />

And the onlyNumbers method would be:

onlyNumbers(e) {
    if((e.keyCode < 48 || e.keyCode > 57) && (e.keyCode < 96 || e.keyCode > 105)) {
        e.preventDefault();
}

Related Links: keyCode values for numeric keypad, preventDefault, Keydown Event

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!