最小/最大验证-Vuetify

时间:2019-07-01 12:27:13

标签: vue.js vuetify.js

在将某些规则添加到我拥有的文本字段时遇到一些问题。

我需要确保只能输入5,000到50,000之间的数字。我当前正在使用以下代码,但收效甚微。

  rules: {
     required: value => !!value || 'Required.',
     loanMin: value => value <= 5000 || 'Loan should be above £5000',
     loanMax: value => value >= 50000 || 'Max should not be above £50,000',
   }

将规则应用于字段如下:

 <v-text-field height="5" :rules="[rules.loanMin, rules.loanMaxMax, rules.required]" editable v-model="sliderLoan" @change="principleLogger(sliderLoan)" persistent-hint outline label="Loan Amount" type="number"></v-text-field>

有什么方法可以将多个规则应用于一个字段?

6 个答案:

答案 0 :(得分:2)

在您的模板中

<v-text-field
    label="Example" 
    v-model="example" 
    :rules="exampleRules"
></v-text-field>

数据中

example: "",
exampleRules: [ 
    v => !!v || "This field is required",
    v => ( v && v >= 5000 ) || "Loan should be above £5000",
    v => ( v && v <= 50000 ) || "Max should not be above £50,000",
],

参考: https://vuetifyjs.com/en/components/forms/,在游乐场下查看示例代码。

奖金::不是问题的一部分,而是相关的,稍作更改后,您可以将其用于最小/最大长度验证。

example: "",
exampleRules: [ 
    v => !!v || "This field is required", 
    v => ( v && v.length >= 10 ) || "This field must have atleast 10 characters",
    v => ( v && v.length <= 100 ) || "This field exceeds maximum allowed characters",
],

答案 1 :(得分:2)

我找到了另一个解决方案。

<v-text-field
    v-model="myNumber"
    :min="minValue"
    :max="maxValue"
    hide-details
    single-line
    type="number"
/>

在您的数据中定义 minValue 和 maxValue 的位置。

答案 2 :(得分:1)

Vue:

<v-text-field 
    height="5" 
    :rules="rules" 
    editable 
    v-model="sliderLoan" 
    @change="principleLogger(sliderLoan)" 
    persistent-hint 
    outline 
    label="Loan Amount" 
    type="number"
/>

脚本:

rules: [
         v => !!v || 'Required',
         v => v >= 5000 || 'Loan should be above £5000',
         v => v <= 50000 || 'Max should not be above £50,000',
    ],

答案 3 :(得分:1)

需要自定义文本字段规则,并已提交“自定义最小和最大”。

<v-text-field
  height="5"
  :rules="[rules.loanMin(sliderLoan,5000,'USD'), rules.loanMaxMax(sliderLoan,50000,'USD')]"
  editable
  v-model="sliderLoan"
  @change="principleLogger(sliderLoan)"
  persistent-hint
  outline
  label="Loan Amount"
  type="number"
></v-text-field>
rules: {
   loanMin(value,min,currency) {
        return (value || "") >= min || `Loan must be at least ${min} {currency}`;
   },
   loanMaxMax(value,max,currency) {
        return (value || "") <= max || `Loan may not be greater than ${max} ${currency}.`;
   }
}

答案 4 :(得分:0)

您有一些错误。您有rules.loanMaxMax,,应该为rules.loanMax,。另外,您的规则也需要颠倒符号:

rules: {
   required: value => !!value || 'Required.',
   loanMin: value => value >= 5000 || 'Loan should be above £5000',
   loanMax: value => value <= 50000 || 'Max should not be above £50,000',
 }

请参见codepen

答案 5 :(得分:0)

我在this这样的页面中回答了这个问题。


只需将v-model变量作为第二个参数传递给您的规则。

<template>
  <v-input :rules=[rules.min(20, field1), rules.max(200, field1)] v-model="field1" />
</template>

<script>    
  data() {
    rules: {
      min(min, v) { 
        return (v || '').length >= min || `Value must be at least ${min}`;
      },

      max(max, v) { 
        return (v || '').length <= max || `Value may not be greater than ${max}.`;
      }
    }
  }
</script>