[Vuejs]-Name Alpha Validations Not Allowing Spaces

0👍

It’s because you have v-model.trim in your input

<input
   v-model.trim="$v.name.$model"
   name="name"
   label="Name"
   :error="$v.name.$error"
   @input="delayTouch($v.name)"
/>

.trim trims the white space, it’s not because of the regex.

0👍

Your regex is close to being correct:

Try this

isNameValid: helpers.regex('isNameValid',/^[a-z0-9_ ]*$/i),

All you were missing is the start and end slashes for the expression, these are needed for JavaScript to know that you’re referring to a regular expression – more information on that can be found here: https://stackoverflow.com/a/32120870/8298506

I also removed the A-Za-z and replaced it with the /i flag which is the case insensitive flag which will result in [A-Z] matching [A-Za-z]

It is worth noting that [A-Za-z0-9_ ] is equivalent shorthand [\w ]

Please understand that these will ONLY match A-Za-z, it will not accept accented characters, or Greek, or Russian etc. If internationalisation support is important to you, you’ll need to look up a solution for regexp-unicode – details of which are a little outside the scope of this answer, but here is a starting point https://javascript.info/regexp-unicode

Leave a comment