Search code examples
vue.jsvuejs2vuetify.jsrules

Vuetify v-text-field check for non digit entry


I want to check whether in v-text-fieldthe user has entered anything besides digit and show the error directly with the rules.

The error should also be shown for (+, -, . , ,)

With:

integerValueCheck: [
    v => /^\d+$/.test(v) || 'Nur Zahlen erlaubt',
],

the error is also shown when the user has entered something in the text field but has removed it again (empty). Thats not ok. The error should not show then


Solution

  • You must check if value is empty:

    integerValueCheck: [
      v => !v || /^\d+$/.test(v) || 'Nur Zahlen erlaubt',
    ]
    

    or

    integerValueCheck: [
      v => /^\d*$/.test(v) || 'Nur Zahlen erlaubt',
    ]