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

211
Views
Vuetify : v-text-field input restore value

It's a simple app with v-text-field for input. I want to enable user input for only numbers, if I try to type like 11a and then press tab or click mouse out of input before I lost focus I see 11, but then focus lost I see 11a. I don't understand how last symbol restored and how fix it. What am I doing wrong?

  <div id="app">
    <h1>{{message}}</h1>

    <v-text-field @input.native="setOnlyNumber" />
  </div>
</template>

<script>
export default {
  data() {
    return {
      message: 'Welcome to Vue!'
    };
  },
  methods: {
    setOnlyNumber(e) {
      e.target.value = e.target.value.replace(/\D+/, '');
    }
  }
};
</script>```

It is my example app on codepen
https://codepen.io/aleksandra973/pen/BamYdOO
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use type="number" in <v-text-field> element.

Working Demo :

Vue.use(Vuetify);

var vm = new Vue({
    el: "#app",
  data() {
    return {
      message: 'Welcome to Vue!'
    };
  }
});
<link href="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js"></script>

<div id="app">
  <h1>{{message}}</h1>
  <v-text-field type="number" label="Number Input Field"/>
</div>

As per the comment by author, Adding below working code snippet which will accept both number and alphabets only.

Vue.use(Vuetify);

var vm = new Vue({
    el: "#app",
  data() {
    return {
      message: 'Welcome to Vue!'
    };
  },
  methods: {
    validateInput(e) {
      let char = String.fromCharCode(e.keyCode); // Get the character
      if(/^[0-9A-Za-z]+$/.test(char)) return true; // Match with regex 
      else e.preventDefault(); // If not match, don't add to input text
    }
  }
});
<link href="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js"></script>

<div id="app">
  <h1>{{message}}</h1>
  <v-text-field type="text" label="Number Input Field" v-on:keypress="validateInput($event)"/>
</div>

about 4 years ago · Juan Pablo Isaza Report

0

The easiest way would be to make use of the Number type input of HTML5.

v-text-field can use this type with the following flag:

<v-text-field type='number' />

Alternatively, you could bind to the data and run a validation as follows:

<template>
  <div id="app">
    <h1>{{message}}</h1>

    <v-text-field v-model='number' @change="validate" />
  </div>
</template>

<script>
export default {
  data() {
    return {
      message: 'Welcome to Vue!',
      number: null,
    };
  },
  methods: {
    validate() {
      this.number = this.number.toString().replace(/\D+/, '');
    }
  }
};
</script>
about 4 years ago · Juan Pablo Isaza 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!