layui modified form validation rules

Use layui form validation form.js, once add validation, such as lay-verify = "phone", lay-verify = "email" will demand mandatory, but many times I can not fill the requirements are, but you need to fill in in accordance with the rules to fill in, so the authentication method offered native form.js not meet the requirements, we need to implement such a function would need to implement, we only need to modify layui following form.js in the corresponding regular expression " / " followed by ' (^ $) | ', like this:

verify: {
        required: [/[\S]+/, "必填项不能为空"],
        phone: [/(^$)|^1\d{10}$/, "请输入正确的手机号"],
        email: [/(^$)|^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/, "邮箱格式不正确"],
        url: [/(^$)|(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/, "链接格式不正确"],
        number: function(e) {
                if (!e || isNaN(e)) return "只能填写数字"
        },
        date: [/(^$)|^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/, "日期格式不正确"],
        identity: [/(^$)|(^\d{15}$)|(^\d{17}(x|X|\d)$)/, "请输入正确的身(和)份(谐)证号"]
}

Guess you like

Origin blog.51cto.com/1197822/2472186