使用的layui,种种原因想自定义执行表单验证方法,发现没有该功能,琢磨了会儿form.js找到了破解之法儿。
新增代码,基本上全是form原有的。
form.js:
/**
* z:表单所在容器id
* return:是否通过验证
**/
u.prototype.doVerify=function(z){
var e = null,
a = f.config.verify,
s = "layui-form-danger",
c = t("#"+z),
d = c.find("*[lay-verify]");
layui.each(d, function (l, r) {
var o = t(this),
c = o.attr("lay-verify").split("|"),
u = o.attr("lay-verType"),
d = o.val();
if (o.removeClass(s), layui.each(c, function (t, l) {
var c, f = "", v = "function" == typeof a[l];
if (a[l]) {
var c = v ? f = a[l](d, r) : !a[l][0].test(d);
if (f = f || a[l][1], "required" === l && (f = o.attr("lay-reqText") || f), c) return "tips" === u ? i.tips(f, function () {
return "string" == typeof o.attr("lay-ignore") || "select" !== r.tagName.toLowerCase() && !/^checkbox|radio$/.test(r.type) ? o : o.next()
}(), {tips: 1}) : "alert" === u ? i.alert(f, {title: "提示", shadeClose: !0}) : i.msg(f, {
icon: 5,
shift: 6
}), n.android || n.ios || setTimeout(function () {
r.focus()
}, 7), o.addClass(s), e = !0
}
}), e) return e
});
return !e;
}
使用.js:
<html>
忽略.....
</html>
<script>
layui.use(['form'],function(){
let form=layui.form;
if(form.doVerify('xxx')){
alert('验证通过');
}else{
alert(验证失败);
}
})
</script>