Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create form number input and validate it

Tags:

vuetify.js

How do I validate a number input field for positive integers correctly? I use these rules

  numberRules: [
    v => v.length > 0 || 'This field may not be empty',
    v => Number.isInteger(v) || 'The value must be an integer number',
    v => v > 0 || 'The value must be greater than zero'
  ]

but get wrong results. I created an example showing my wrong behaviour

https://codesandbox.io/s/vue-with-vuetify-eagles-yqxcc

To modify the textfield as a number input I added type="number" as a component property.

like image 914
Question3r Avatar asked Feb 28 '26 08:02

Question3r


1 Answers

You get wrong result because v in your rules methods is actually string (yes yes, fun fact :) ).

You can:

  1. change your rule to v => Number.isInteger(Number(v)) || "The value must be an integer number",

  2. add .number modifier to v-model=... to looks like v-model.number=.... But in that case you first rule method wouldn't work, because v as number doesn't have .length

like image 176
Đorđe Zeljić Avatar answered Mar 06 '26 16:03

Đorđe Zeljić



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!