1

How to check a form for a regex rule by using Vuelidate. It is clear from the guide that you need to use "helpers", however, when checking for the validity of a regular expression, Vuelidate always answers true. My code:

    <template>
    <div class="form-wrapper">
        <h1 class="form-title">Зарегистрируйтесь</h1>
        <form action="" class="form">
            <div class="form-item">
                <input type="text"
                class="form-item-input"
                placeholder="Введите отображаемый логин на сайте"
                v-model="userName"
                @click="console.log(v$.userName.regular.$response)"
                @blur="v$.userName.$touch"
                :class="{
                            alert: (!v$.userName.required.$response && v$.userName.$dirty)
                        }"
                >
            </div>
            <button class="form-item-submit" type="submit">Войти</button>
        </form>
    </div>
</template>

<script lang="ts">
    import { useVuelidate } from '@vuelidate/core';
    import { required, helpers } from '@vuelidate/validators';
    const regular = helpers.regex(/^.*(?=.*[a-zA-Z])(?=.*\d)(?=.*[!#$%&? "]).*$/)
    export default {
        setup() {
            return {
                v$: useVuelidate()
            }
        },
        data() {
            return {
                userName: '',
            }
        },
        validations() {
            return {
                userName: { required, regular }
            }
        }
    }
</script>

<style scoped>

</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/3.5.4/vue.global.min.js"></script>

1 Answer 1

0

Your code seems fine to me and matches the documentation. In order to offer a solution:

You don't have to use the vuelidate regex helper. You can also use your own validation functions like this:

function bic(value: string) {
  if (!value) return true;

  const pattern = new RegExp('^$|([a-zA-Z]{4}[a-zA-Z]{2}[a-zA-Z0-9]{2}([a-zA-Z0-9]{3})?)');
  return pattern.test(value);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.