6

My goal is to limit the number of digit user can enter in the input field. So a user can only input 10 digits. I tried min and max still doesn't work

Here's the code

        <input
          v-model="amount"
          type="number"
        >


        <script>
             export default {
                  data() {
                      return { 
                          amount: 7800
                      }
                   }

              }

        </script>

Right now i can add more than 10 digits

1

7 Answers 7

34

Replace this:

<input v-model="amount" type="number">

to

<input v-model="amount"
        oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);"
        type = "number"
        maxlength = "10"
/>
Sign up to request clarification or add additional context in comments.

1 Comment

Great, been searching for this for several hours.
24

We can control the value of <input> manually:

 <input
  type="number"
  :value="amount"
  @input="updateValue"
/>

and check in updateValue method:

  data() {
    return {
      amount: 7800
    }
  },
  methods: {
    updateValue(event) {
      const value = event.target.value
      console.log(value, this.amount)
      if (String(value).length <= 10) {
        this.amount = value
      }
      this.$forceUpdate()
    }
  }

Note that this.$forceUpdate() is used to make component re-render when user input more than 10 characters.

Demo on Codepen

4 Comments

This doesnt work if i put v-model="amount" which I need to put
I updated the answer. v-model is a shorthand for :value and @input
I see, weird that v-model doesnt work for @input but :value does work thanks
This answer solved my issue, but I can't run away from a thought that this is a hack. Documentation says If you find yourself needing to force an update in Vue, in 99.99% of cases, you’ve made a mistake somewhere.. Is there a better way to solve this issue in 2021?
3

You can also do this inline:

<input type='number' v-model='trademark' class='form-control' @input="trademark = trademark.slice(0,10)"/>

Comments

2

Put a condition on Updated, checking the digits, if surpasses it, cut it. I did something similar, but for max value, not digits, but i think its the same logic:

updated() {
    if (this.album.max_colorized > this.maxPictures) {
        this.album.max_colorized = this.maxPictures;
    }
}

This way if it surpasses the max, change to max.

Comments

1

Here in 2023 and I realized that there's no need to listen to an input event or use the updated hook.

Simply use the maxlength attribute for form inputs. An example of usage is:

 <input type="text" maxlength="10" v-model="myModel" />

If you need the length to be dynamic, then use this:

<input type="text" :maxlength="max" v-model="myModel" />

data: () => ({
      max: 10
 })

6 Comments

OP can't just use maxlength because their input type is number.
@ChristineTreacy have you been able to limit the digit number on the input field? I believe it would also work on input type of number
I came looking for a solution because maxlength does not work when they input type = number. Here's the solution I posted: <input type='number' v-model='trademark' class='form-control' @input="trademark = trademark.slice(0,10)"/>
wow this looks great @ChristineTreacy. please share the link to the post so I can give an upvote
Thanks! Glad it worked for you. It did post it the other day. Appreciate the upvote! stackoverflow.com/a/76092625/12488167
|
1

maxlength works with type="text" it seems not to work only with type="number" and it was designed this way. So if it doesn't matter you could go with type="tel" and it should be ok, otherwise only JS

Comments

0
<input
 type="number"
 v-model="slot"
 required
 @change="(e) => handleChange(e, 1, 48)"
/>
<script setup>
const handleChange = (e, min, max) => {
  if (+e.target.value > max) {
    slot = max
  } else if (+e.target.value < min) {
    slot = min
  }
}
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.