tempra
tempra

Reputation: 2331

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?

Upvotes: 1

Views: 1863

Answers (1)

schutte
schutte

Reputation: 2166

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) }}

Upvotes: 2

Related Questions