最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove white space from v-text-field Vue - Stack Overflow

programmeradmin2浏览0评论

I have a v-text-field in vue, and I will like to remove all white spaces or prevent any white space from it. For instance 'Project Number' input should be 'ProjectNumber'.

I have a function but this doesn't work, I tried keyup too.

<v-text-field
  @input='removeWhiteSpace(row.newCol)'
  outlined
  dense
  color="primary"
  style="width: 200px;"
  v-model="row.newCol">
</v-text-field>

removeWhiteSpace(text) {
  return text.replace(/[\s\/]/g, '');
}

I have a v-text-field in vue, and I will like to remove all white spaces or prevent any white space from it. For instance 'Project Number' input should be 'ProjectNumber'.

I have a function but this doesn't work, I tried keyup too.

<v-text-field
  @input='removeWhiteSpace(row.newCol)'
  outlined
  dense
  color="primary"
  style="width: 200px;"
  v-model="row.newCol">
</v-text-field>

removeWhiteSpace(text) {
  return text.replace(/[\s\/]/g, '');
}
Share Improve this question edited Apr 5, 2022 at 13:59 Penny Liu 17.4k5 gold badges86 silver badges108 bronze badges asked Jul 20, 2020 at 17:36 teestunnateestunna 2171 gold badge6 silver badges19 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 10

There is a native vue modifier for that, v-model.trim

<v-text-field
  outlined
  dense
  color="primary"
  style="width: 200px;"
  v-model.trim="row.model">
</v-text-field>

Official documentation: https://vuejs.org/guide/essentials/forms#trim

You can do this:

<v-text-field
  @keydown.space.prevent
  outlined
  dense
  color="primary"
  style="width: 200px;"
  v-model="row.newCol">
</v-text-field>

From here: https://stackoverflow.com/a/53521999/10642485

For this, I'd recommend skipping v-model and just directly assign the transformed value

<v-text-field
  :value="row.newCol"
  @input="row.newCol = removeWhiteSpace($event)"
  outlined
  dense
  color="primary"
  style="width: 200px;">
</v-text-field>

This could be helpful for the use case of accepting a number:

If you want user input to be automatically typecast as a number, you can add the number modifier to your v-model managed inputs:

<input v-model.number="age" />

If the value cannot be parsed with parseFloat(), then the original value is used instead.

The number modifier is applied automatically if the input has type="number".

Source: https://vuejs.org/guide/essentials/forms#number

发布评论

评论列表(0)

  1. 暂无评论