2018-09-16 19:33:56 +08:00
|
|
|
|
(function ($) {
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
var Validate = function (element, options) {
|
|
|
|
|
var that = this;
|
|
|
|
|
this.$element = $(element);
|
|
|
|
|
this.options = $.extend({
|
|
|
|
|
pendingRequest: 0,
|
|
|
|
|
pending: {},
|
|
|
|
|
successList: [],
|
|
|
|
|
optional: function () { return false; },
|
|
|
|
|
invalid: {},
|
|
|
|
|
getLength: function (value, element) {
|
|
|
|
|
switch (element.nodeName.toLowerCase()) {
|
|
|
|
|
case "select":
|
|
|
|
|
return $("option:selected", element).length;
|
|
|
|
|
case "input":
|
|
|
|
|
if (this.checkable(element)) {
|
2018-11-21 16:19:14 +08:00
|
|
|
|
return $(element).filter(":checked").length;
|
2018-09-16 19:33:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return value.length;
|
|
|
|
|
},
|
|
|
|
|
checkable: function (element) {
|
|
|
|
|
return (/radio|checkbox/i).test(element.type);
|
|
|
|
|
},
|
|
|
|
|
depend: function (param, element) {
|
|
|
|
|
return this.dependTypes[typeof param] ? this.dependTypes[typeof param](param, element) : true;
|
|
|
|
|
},
|
|
|
|
|
dependTypes: {
|
|
|
|
|
"boolean": function (param) {
|
|
|
|
|
return param;
|
|
|
|
|
},
|
|
|
|
|
"string": function (param, element) {
|
|
|
|
|
return !!$(param, element.form).length;
|
|
|
|
|
},
|
|
|
|
|
"function": function (param, element) {
|
|
|
|
|
return param(element);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
previousValue: function (element, method) {
|
|
|
|
|
method = typeof method === "string" && method || "remote";
|
|
|
|
|
return $.data(element, "previousValue") || $.data(element, "previousValue", {
|
|
|
|
|
old: null,
|
|
|
|
|
valid: true,
|
|
|
|
|
message: "请修正本字段"
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
startRequest: function (element) {
|
|
|
|
|
if (!this.pending[element.name]) {
|
|
|
|
|
this.pendingRequest++;
|
|
|
|
|
$(element).addClass(this.settings.pendingClass);
|
|
|
|
|
this.pending[element.name] = true;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
stopRequest: function (element, valid) {
|
|
|
|
|
this.pendingRequest--;
|
|
|
|
|
|
|
|
|
|
// Sometimes synchronization fails, make sure pendingRequest is never < 0
|
|
|
|
|
if (this.pendingRequest < 0) {
|
|
|
|
|
this.pendingRequest = 0;
|
|
|
|
|
}
|
|
|
|
|
delete this.pending[element.name];
|
|
|
|
|
$(element).removeClass(this.settings.pendingClass);
|
|
|
|
|
},
|
|
|
|
|
showErrors: function (errors) {
|
|
|
|
|
for (var name in errors) {
|
|
|
|
|
var element = document.getElementById(name);
|
|
|
|
|
var $element = $(element);
|
|
|
|
|
that.tooltip.call(that, element, false);
|
|
|
|
|
$element.attr('data-original-title', errors[name]).tooltip('show');
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
defaultMessage: function (element, rule) {
|
|
|
|
|
return that.defaultMessage(element, rule);
|
|
|
|
|
},
|
|
|
|
|
resetInternals: function () { },
|
|
|
|
|
errorsFor: function (element) {
|
|
|
|
|
that.tooltip.call(that, element, true);
|
|
|
|
|
},
|
|
|
|
|
settings: $.validator.defaults
|
|
|
|
|
}, this.defaults(), options);
|
2018-11-21 16:19:14 +08:00
|
|
|
|
|
|
|
|
|
// fix bug Edge
|
|
|
|
|
this.$element.find('select' + this.options.childClass).on('input', function (e) {
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
}).on('change', function () {
|
|
|
|
|
$(this).trigger('input.lgb.validate');
|
|
|
|
|
});
|
|
|
|
|
|
2018-09-16 19:33:56 +08:00
|
|
|
|
this.$element.on('input.lgb.validate', this.options.childClass, function () {
|
|
|
|
|
if (!that.validElement(this)) $(this).tooltip('show');
|
|
|
|
|
}).on('inserted.bs.tooltip', this.options.childClass, function () {
|
|
|
|
|
$('#' + $(this).attr('aria-describedby')).addClass(that.options.errorClass);
|
|
|
|
|
});
|
2018-11-21 20:07:55 +08:00
|
|
|
|
|
2018-09-16 19:33:56 +08:00
|
|
|
|
if (!this.options.validButtons) return;
|
|
|
|
|
this.$element.find(this.options.validButtons).on('click.lgb.validate', function (e) {
|
|
|
|
|
var valid = that.valid();
|
|
|
|
|
$(this).attr(Validate.DEFAULTS.validResult, valid);
|
|
|
|
|
if (!valid) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopImmediatePropagation();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if (this.options.modal) {
|
|
|
|
|
$(this.options.modal).on('show.bs.modal', function (e) {
|
|
|
|
|
that.reset();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Validate.VERSION = '2.0';
|
|
|
|
|
|
|
|
|
|
Validate.DEFAULTS = {
|
|
|
|
|
validClass: 'is-valid',
|
|
|
|
|
errorClass: 'is-invalid',
|
|
|
|
|
ignoreClass: '.ignore',
|
|
|
|
|
childClass: '[data-valid="true"]',
|
|
|
|
|
validResult: 'data-valid-result'
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Validate.prototype.defaults = function () {
|
|
|
|
|
return $.extend(Validate.DEFAULTS, {
|
|
|
|
|
validButtons: this.$element.attr('data-valid-button'),
|
|
|
|
|
modal: this.$element.attr('data-valid-modal')
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Validate.prototype.reset = function () {
|
|
|
|
|
var css = this.options.validClass + ' ' + this.options.errorClass;
|
|
|
|
|
this.$element.find(this.options.childClass).each(function () {
|
|
|
|
|
var $this = $(this);
|
|
|
|
|
$this.tooltip('dispose');
|
|
|
|
|
$this.removeClass(css);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Validate.prototype.valid = function () {
|
|
|
|
|
var that = this;
|
|
|
|
|
var op = this.options;
|
|
|
|
|
var $firstElement = null;
|
|
|
|
|
|
|
|
|
|
this.$element.find(op.childClass + ':visible').not(op.ignoreClass).each(function () {
|
2018-09-30 15:22:02 +08:00
|
|
|
|
if (!that.validElement(this) && $firstElement === null) $firstElement = $(this);
|
2018-09-16 19:33:56 +08:00
|
|
|
|
});
|
|
|
|
|
if ($firstElement) $firstElement.tooltip('show');
|
2018-09-30 15:22:02 +08:00
|
|
|
|
return $firstElement === null;
|
2018-09-16 19:33:56 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Validate.prototype.validElement = function (element) {
|
|
|
|
|
var result = this.check(element);
|
|
|
|
|
this.tooltip(element, result);
|
2018-09-30 15:22:02 +08:00
|
|
|
|
return result;
|
2018-09-16 19:33:56 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Validate.prototype.tooltip = function (element, valid) {
|
2018-09-30 15:22:02 +08:00
|
|
|
|
if (valid === "pending") return;
|
2018-09-16 19:33:56 +08:00
|
|
|
|
|
|
|
|
|
var op = this.options;
|
|
|
|
|
var $this = $(element);
|
2018-09-30 15:22:02 +08:00
|
|
|
|
if (valid) $this.tooltip('dispose');
|
|
|
|
|
else {
|
|
|
|
|
if (!$this.hasClass(op.errorClass)) $this.tooltip();
|
2018-09-16 19:33:56 +08:00
|
|
|
|
}
|
|
|
|
|
if (!valid) {
|
|
|
|
|
$this.removeClass(op.validClass).addClass(op.errorClass);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$this.removeClass(op.errorClass).addClass(op.validClass);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Validate.prototype.check = function (element) {
|
|
|
|
|
var result = true;
|
|
|
|
|
var $this = $(element);
|
|
|
|
|
if ($this.is(':hidden')) return result;
|
|
|
|
|
var methods = this.rules(element);
|
2019-07-12 12:34:58 +08:00
|
|
|
|
var proxy = function (rule) {
|
2018-09-16 19:33:56 +08:00
|
|
|
|
if ($.isFunction($.validator.methods[rule])) {
|
|
|
|
|
result = $.validator.methods[rule].call(this.options, $this.val(), element, methods[rule]);
|
|
|
|
|
if (!result) {
|
|
|
|
|
$this.attr('data-original-title', this.defaultMessage(element, { method: rule, parameters: methods[rule] }));
|
2019-03-15 17:04:56 +08:00
|
|
|
|
return result;
|
2018-09-16 19:33:56 +08:00
|
|
|
|
}
|
2019-07-12 12:34:58 +08:00
|
|
|
|
|
|
|
|
|
// checkGroup rule
|
|
|
|
|
if (rule === 'checkGroup') {
|
|
|
|
|
var $checkers = this.$element.find(this.options.childClass).filter(function () {
|
|
|
|
|
var $this = $(this);
|
|
|
|
|
return $this.hasClass(rule) || $this.attr(rule);
|
|
|
|
|
});
|
|
|
|
|
$checkers.removeClass(this.options.errorClass).removeClass(this.options.validClass);
|
|
|
|
|
if (result) $checkers.tooltip('dispose');
|
|
|
|
|
else $checkers.addClass(this.options.errorClass).tooltip();
|
|
|
|
|
}
|
2018-09-16 19:33:56 +08:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
console.log('没有匹配的方法 ' + rule);
|
|
|
|
|
}
|
2019-03-15 17:04:56 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
var remote = null;
|
|
|
|
|
for (var rule in methods) {
|
2019-07-12 12:34:58 +08:00
|
|
|
|
if (rule !== 'remote') {
|
2019-03-15 17:04:56 +08:00
|
|
|
|
result = proxy.call(this, rule);
|
2019-07-12 12:34:58 +08:00
|
|
|
|
if (!result) return false;
|
2019-03-15 17:04:56 +08:00
|
|
|
|
}
|
|
|
|
|
else remote = rule;
|
2018-09-16 19:33:56 +08:00
|
|
|
|
}
|
2019-07-12 12:34:58 +08:00
|
|
|
|
if (remote !== null) result = proxy.call(this, remote);
|
2018-09-16 19:33:56 +08:00
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Validate.prototype.defaultMessage = function (element, rule) {
|
2018-09-30 15:22:02 +08:00
|
|
|
|
var message = $(element).attr('data-' + rule.method + '-msg') || rule.method === 'required' && $(element).attr('placeholder') || $.validator.messages[rule.method];
|
2018-09-16 19:33:56 +08:00
|
|
|
|
var theregex = /\$?\{(\d+)\}/g;
|
|
|
|
|
if (typeof message === "function") {
|
|
|
|
|
message = message.call(this, rule.parameters, element);
|
|
|
|
|
} else if (theregex.test(message)) {
|
|
|
|
|
message = $.validator.format(message.replace(theregex, "{$1}"), rule.parameters);
|
|
|
|
|
}
|
|
|
|
|
return message;
|
|
|
|
|
};
|
|
|
|
|
|
2019-07-12 12:34:58 +08:00
|
|
|
|
Validate.prototype.attributeRules = function (element, rules) {
|
|
|
|
|
var $element = $(element), value;
|
2018-09-16 19:33:56 +08:00
|
|
|
|
|
|
|
|
|
$.each(["remote"], function () {
|
2019-07-12 12:34:58 +08:00
|
|
|
|
var para = $element.attr(this);
|
|
|
|
|
if (para) {
|
2018-09-16 19:33:56 +08:00
|
|
|
|
if (element.name === "") element.name = element.id;
|
|
|
|
|
rules[this] = $.formatUrl(para);
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-07-12 12:34:58 +08:00
|
|
|
|
|
|
|
|
|
$.each(["radioGroup", "checkGroup"], function () {
|
|
|
|
|
if (rules[this]) {
|
|
|
|
|
delete rules.required;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
});
|
2018-09-16 19:33:56 +08:00
|
|
|
|
return rules;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Validate.prototype.rules = function (element) {
|
|
|
|
|
var $this = $(element);
|
|
|
|
|
var rules = $this.data('lgb.Validate.Rules');
|
2019-07-12 12:34:58 +08:00
|
|
|
|
if (!rules) $this.data('lgb.Validate.Rules', rules = this.attributeRules(element, $.validator.normalizeRules($.extend(
|
|
|
|
|
{ required: true },
|
|
|
|
|
$.validator.classRules(element),
|
|
|
|
|
$.validator.attributeRules(element)
|
|
|
|
|
))));
|
2018-09-16 19:33:56 +08:00
|
|
|
|
return rules;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function Plugin(option) {
|
|
|
|
|
return this.each(function () {
|
|
|
|
|
var $this = $(this);
|
|
|
|
|
var data = $this.data('lgb.Validate');
|
2018-09-30 15:22:02 +08:00
|
|
|
|
var options = typeof option === 'object' && option;
|
2018-09-16 19:33:56 +08:00
|
|
|
|
|
2018-09-30 15:22:02 +08:00
|
|
|
|
if (!data && /valid|defaults/.test(option)) return;
|
|
|
|
|
if (!data) $this.data('lgb.Validate', data = new Validate(this, options));
|
|
|
|
|
if (typeof option === 'string') data[option]();
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-09-16 19:33:56 +08:00
|
|
|
|
|
|
|
|
|
$.fn.lgbValidate = Plugin;
|
|
|
|
|
$.fn.lgbValidate.Constructor = Validate;
|
|
|
|
|
$.fn.lgbValidator = function () {
|
|
|
|
|
return this.data('lgb.Validate');
|
|
|
|
|
};
|
|
|
|
|
$.fn.lgbValid = function () {
|
|
|
|
|
var $this = this;
|
2018-09-30 15:22:02 +08:00
|
|
|
|
return $this.attr(Validate.DEFAULTS.validResult) === 'true';
|
2018-09-16 19:33:56 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$(function () {
|
2018-09-30 15:22:02 +08:00
|
|
|
|
if ($.isFunction($.validator)) {
|
|
|
|
|
$.validator.addMethod("ip", function (value, element) {
|
|
|
|
|
return this.optional(element) || /^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/.test(value);
|
|
|
|
|
}, "请填写正确的IP地址");
|
2018-11-21 16:19:14 +08:00
|
|
|
|
|
|
|
|
|
$.validator.addMethod("radioGroup", function (value, element) {
|
|
|
|
|
return $(element).find(':checked').length === 1;
|
|
|
|
|
}, "请选择一个选项");
|
2019-07-12 12:34:58 +08:00
|
|
|
|
|
|
|
|
|
$.validator.addMethod("checkGroup", function (value, element) {
|
|
|
|
|
return $(element).parents('[data-toggle="LgbValidate"]').find(':checked').length >= 1;
|
|
|
|
|
}, "请选择一个选项");
|
|
|
|
|
|
2019-03-15 17:07:38 +08:00
|
|
|
|
$.validator.addMethod("userName", function (value, element) {
|
|
|
|
|
return this.optional(element) || /^[a-zA-Z0-9_@.]*$/.test(value);
|
|
|
|
|
}, "登录名称不可以包含非法字符");
|
2019-07-27 14:36:15 +08:00
|
|
|
|
|
|
|
|
|
$.validator.addMethod("greaterThan", function (value, element, target) {
|
|
|
|
|
return this.optional(element) || $(target).val() <= value;
|
|
|
|
|
}, "");
|
|
|
|
|
|
|
|
|
|
$.validator.addMethod("lessThan", function (value, element, target) {
|
|
|
|
|
return this.optional(element) || $(target).val() >= value;
|
|
|
|
|
}, "");
|
2018-09-30 15:22:02 +08:00
|
|
|
|
}
|
2018-09-16 19:33:56 +08:00
|
|
|
|
$('[data-toggle="LgbValidate"]').lgbValidate();
|
|
|
|
|
});
|
|
|
|
|
})(jQuery);
|