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
4 Answers
Reset to default 10There 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 yourv-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 hastype="number"
.
Source: https://vuejs.org/guide/essentials/forms#number