简体   繁体   English

在输入字段上设置最大和最小限制?

[英]Setting max and min limit on input field?

i am having this issue where i have a vuetify text-field and i am trying to set a max and min limit on it.我遇到了这个问题,我有一个 vuetify 文本字段,我正在尝试为其设置最大和最小限制。 Now setting min=0 and max=10 works but seems like you could still paste values more than 10.现在设置 min=0 和 max=10 有效,但似乎您仍然可以粘贴大于 10 的值。

Here's a codepen这是一个代码笔

<div id="app">
  <v-app id="inspire">
    <v-container>
      <v-layout row wrap>
        <v-text-field
         type="number"
         min=0
         max=10
         onkeyup="if(this.value > 10) this.value = 10;">    
        </v-text-field>
      </v-layout>
    </v-container>
  </v-app>
</div>

new Vue({
  el: '#app',
  data() {
    return {
    }
  }
 })

Using the onkeyup works but you can still paste values greater than 10 and if you click outside, the value greater than 10 shows up.使用 onkeyup 有效,但您仍然可以粘贴大于 10 的值,如果您在外部单击,则会显示大于 10 的值。

使用 oninput 并且我不会对值进行硬编码

oninput="if(Number(this.value) > Number(this.max)) this.value = this.max;"

You can put attributes on input element manually (although this solution is not clear, in fact i would call it hack, but it's easy to do)您可以手动将属性放在输入元素上(虽然这个解决方案不清楚,实际上我会称之为hack,但很容易做到)

Put $ref on you field将 $ref 放在您的字段上

<v-text-field ref="myfield" ... >

and then you can modify it as mounted然后你可以修改它为mounted

mounted () {
  const inputElement = this.$refs.myfield.$el.querySelector('input')
  inputElement.min = 0
  inputElement.max = 10
}

You can also modify any other attribute like step , or maxLength (for text type).您还可以修改任何其他属性,如stepmaxLength (用于文本类型)。 Refer input element API https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement参考输入元素 API https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement

You can use data rules for this as well.您也可以为此使用数据规则。

Eg: Inside data例如:内部数据

data(){
organization: "",
      organizationRules: [
        (v) => !!v || "Organization is required",
        (v) =>
          (v && v.length <= 50) ||
          "Organization must be less than 50 characters",
      ],
}

Inside template内模板

<template>
<v-text-field
          v-model="organization"
          :counter="50"
          :rules="organizationRules"
          label="Organization"
          required
        ></v-text-field>
</template>

改用onblur以便在输入变得不集中时,代码会运行以将其设置为最大值

It is possible to use a watcher for this issue.可以对这个问题使用观察者。 I don't know if It is the best way but It works.我不知道这是否是最好的方法,但它有效。

Codepen代码笔

<div id="app">
  <v-app id="inspire">
    <v-container>
      <v-layout row wrap>
        <v-text-field
         v-model="number"
         type="number"
        min=0
         max=10>    
        </v-text-field>
      </v-layout>
    </v-container>
  </v-app>
</div>

new Vue({
  el: '#app',
  data() {
    return {
      number: 0
    }
  },
  watch: {
    number (val, old) {
      console.log(val, old)
      if (+val > 10) {
        this.number = 10
      }
    }
   }
})

If you want to have maximum 2 digits before and after the decimals then here's the implementation for that如果你想在小数点前后最多有 2 位数字,那么这里是实现

template模板


<v-text-field v-model="input" v-on:keyup="handleInput" type="number" oninput="if(Number(this.value) > Number(this.max)) this.value = this.max;" max="99" ></v-text-field>

script脚本

 handleInput(e) {
      let stringValue = e.target.value.toString();

      let regex = /^\d{0,2}(?:\.\d{1,2})?$/;
      if (!stringValue.match(regex) && this.budget !== "") {
        this.budget = this.previousPrice;
      }
      this.previousPrice = this.budget;
    },

you can customize the regex according to your need您可以根据需要自定义正则表达式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM