合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
## 示例代码及注释如下: ```javascript layui.use(["validate"],function (){ let validate = layui.validate; // 待验证数据 let data = { name:"小王", age:"29", mobile:"123456", }; // 待验证数据对应title,为用户增强提示的可阅读性(非必选)。若设置对应规则的message提示,可为空 let dataTitle = { name:"姓名", age:"年龄", mobile:"手机" }; // 为验证器设置参数 validate.rule({ "name":"require|chs", "age":"require|positiveInteger|isAge:18", "mobile":"require|mobile", }).message({ "name.require":"姓名不能为空", "name.chs":"姓名只能为汉字", "age.require":"年龄不能为空", "age.positiveInteger":"年龄需为正整数", "mobile.require":"手机不能为空", "mobile.mobile":"手机格式错误", }).scene({ "scene1":["name","mobile"], "scene2":["name","age","mobile"], }).methods({ isAge:(value, rule = '', data={})=>{ if(rule.length<=0) return "规则[isAge]错误"; let maxAge = rule | 0; return value<=maxAge ? true : '年龄不能超过18岁'; } }); // 验证环境scene1,只校验name、mobile let res1 = validate.check(data,dataTitle,"scene1"); console.log(res1); console.log(validate.getError()); // 验证全部参数,方法1 let res2 = validate.check(data,dataTitle,"scene2"); console.log(res2); console.log(validate.getError()); // 验证全部参数,方法2 let res3 = validate.check(data,dataTitle); console.log(res3); console.log(validate.getError()); // 验证全部参数,当验证失败时自动弹出错误提示 let res4 = validate.checkTips(data,dataTitle); console.log(res4); console.log(validate.getError()); }); ```