本文介绍了如何在 vuelidate 的验证中使用条件运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚刚安装了 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}/);
我的目标是,输入框应该只接受 email
或 phone_no
,我尝试了下面的解决方案,但没有奏效.
My objective is, the input box should accept only email
or phone_no
, I tried the solution(s) below but none works.
太阳.1
validations: { username: { valid: phone or email }}
太阳.2
validations: { username: { valid: phone || email }}
有人知道如何实现这一目标吗?
Someone knows how to achieve this?
推荐答案
哦,我想出了解决方案!
Ohws I've figured out the solution!
您首先需要导入or
(您的情况)或and
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) }}
这篇关于如何在 vuelidate 的验证中使用条件运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!