[Vuejs]-How do I set the user can not enter the number 0 in the first number?

2👍

Using a regex:

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data: () => ({
    text: null
  }),
  methods: {
    handler(val){
        this.text = val.replace(/^[^1-9]+/, '')
    }
  }
})

1👍

Demo You are looking directly whole enter. just check first letter

if(val.substr(0,1)==="0"){
    this.text=val.slice(1);
}

1👍

If you want the preserve the string, like if someone types 852 and then you add the 0 in front, and you want it to change back to 852 rather than null.

Like change 0852 -> 852 if the zero was added later.

Then you should update your handler function

handler(val){
      if(val==="0"){
         this.text=null;
      }
      if(val[0] === "0"){
        this.text = this.text.substr(1);
      }
    }

👤AKT

0👍

You can check if the first char is "0" like this:

 if(val[0] === "0") {
    this.text = null;
 }
👤zb22

Leave a comment