0

I have an input in VueJS which is bound using v-model e.g.

<input type="number" v-model.number="testinput">

I want to place a restriction on this input, so that it can only be a single digit, between 1 and 6. As soon as a single digit is in the box, additional key presses should have no effect. Entering an invalid digit such as 9 should also have no effect.

I'm not sure what the best approach to this would be, do I need a directive, filter, event handler, or maybe make a new custom input component?

1 Answer 1

1
  1. Set the maxlength property to 1 on the input field (this will restrict to single characters).

  2. Add the proper regex to the input field using the pattern property. (This will check that the input has only 1 character, and is using numbers 1-5.

     <input type="number" maxlength="1" pattern="([12345])\w{0}" v-model.number="testinput">
    
Sign up to request clarification or add additional context in comments.

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.