// 定义常用的校验,常用的正则 https://www.open-open.com/code/view/1430625516632 layui.define(['jquery'], function (exports) { var $ = layui.jquery; exports('validate', { username: function (value, item) { if (!isEmpty(value)) { var result = ''; $.ajax({ url: ctx + 'user/check/' + value, data: { "userId": item.getAttribute('id') }, async: false, type: 'get', success: function (d) { (!d) && (result = '该用户名已存在') } }); if (!isEmpty(result)) { return result; } } }, cron: function (value, item) { if (!isEmpty(value)) { var result = ''; $.ajax({ url: ctx + 'job/cron/check', data: { "cron": value }, async: false, type: 'get', success: function (d) { (!d) && (result = 'cron表达式不合法') } }); if (!isEmpty(result)) { return result; } } }, email: function (value) { if (!isEmpty(value)) { if (!new RegExp("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$").test(value)) { return '请填写正确的邮箱'; } } }, phone: function (value) { if (!isEmpty(value)) { if (!new RegExp("^1\\d{10}$").test(value)) { return '请填写正确的手机号码'; } } }, number: function (value) { if (!isEmpty(value)) { if (!new RegExp("^[0-9]*$").test(value)) { return '只能填写数字'; } } }, range: function (value, item) { var minlength = item.getAttribute('minlength') ? item.getAttribute('minlength') : -1; var maxlength = item.getAttribute('maxlength') ? item.getAttribute('maxlength') : -1; var length = value.length; if (minlength === -1) { if (length > maxlength) { return '长度不能超过 ' + maxlength + ' 个字符'; } } else if (maxlength === -1) { if (length < minlength) { return '长度不能少于 ' + minlength + ' 个字符'; } } else { if (length > maxlength || length < minlength) { return '长度范围 ' + minlength + ' ~ ' + maxlength + ' 个字符'; } } } }); function isEmpty(obj) { return typeof obj == 'undefined' || obj == null || obj === ''; } });