Search code examples
vue.jsvuejs2vuelidate

How to use conditional operator inside validations in vuelidate?


I just installed vuelidate, and created a helper that checks if the value is phone no. reference

import { helpers } from 'vuelidate/lib/validators';
const phone = helpers.regex('alpha', /^(09)[0-9]{9}/);

My objective is, the input box should accept only email or phone_no, I tried the solution(s) below but none works.

Sol. 1

validations: { username: { valid: phone or email }}

Sol. 2

validations: { username: { valid: phone || email }}


Someone knows how to achieve this?


Solution

  • Ohws I've figured out the solution!

    You need first to import the or (your case) or and

    import { email, or, and } from 'vuelidate/lib/validators';
    

    Lastly, your mistake is, You should use it as a function. Instead of the solutions above, it should be these codes below.

    validations: { username: { valid: or(email, phone) }}