vuelidate框架自定义验证规则【注意事项】

官方文档:https://vuelidate.netlify.com/#sub-props-support

参考别人的用法:https://www.jianshu.com/p/6d5ad3058e1b

本人案例总结

// 正则表达式规则【regex第一个参数随意,可用来标注正则表达式的条件】
const username = helpers.regex( "用户名必须是字母开头,最少3位,最多16位", /^[a-zA-Z]\w{2,15}$/ );
const mobile = helpers.regex( "手机号必须1开头的11位数字", /^1\d{10}$/ );

// vuelidate自定义不带参数的校验规则【withParams第一个参数是对象,可以使用$v.indexof.$param.rule来访问】
const indexof = helpers.withParams(
    { type: "rule", value: "必须包含cool字符串" },
    ( value ) => !helpers.req( value ) || value.indexOf( "cool" ) >= 0,
);

// vuelidate自定义带带参数的校验规则【withParams第一个参数是对象,可以使用$v.indexof_param.$param.rule来访问】
const indexof_param = ( param ) => helpers.withParams(
    { type: "rule", value: param },
    ( value ) => !helpers.req( value ) || value.indexOf( param ) >= 0,
);

猜你喜欢

转载自blog.csdn.net/weixin_43343144/article/details/89676013