Home  >  Q&A  >  body text

Reserve zero as first character of HTML input

So I have a problem with HTML input in Vue. Whenever I try to enter with zero as the first character, the input value changes. For example, if I want to enter '005', it will become '5', and similarly for '05' it will become '5'. I've tried looking up information about it, but all I've found is how to implement it, not how to prevent it. Any ideas?

I tried using e.preventDefauld(), but e is not defined. I also tried converting the number to a string on input.

P粉014218124P粉014218124409 days ago553

reply all(1)I'll reply

  • P粉604848588

    P粉6048485882023-09-07 10:35:07

    The following should work as you expect

    <template>
      number: {{ number }}
      <input :value="number" @input="updateNumber" type="number">
    </template>
    
    <script>
    export default {
      data() {
        return {
          number: 12
        }
      },
      methods: {
        updateNumber(e) {
          this.number = e.target.value
        }
      }
    }
    </script>
    

    This isa playground.

    reply
    0
  • Cancelreply