How to use conditional operator inside validations in vuelidate?

我怕爱的太早我们不能终老 提交于 2020-12-12 02:46:24

问题


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?

回答1:


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


来源:https://stackoverflow.com/questions/60866820/how-to-use-conditional-operator-inside-validations-in-vuelidate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!