//Juqery 验证初始化 var Validation = { Initial: function ($form) { var $from = $form == null ? $("form.needs-validation") : $form.find("form.needs-validation"); $from.validate({ errorClass: "valid-error",//错误提示的css类名 errorElement: "div",//自定义错误标记标签 errorPlacement: function (error, element) { if (element.hasClass("select2")) element.next(".select2-container").after(error); else element.after(error); } }); //重构默认验证消息 $.validator.messages.required = "该项是必填项"; $.validator.messages.digits = "请输入正确的整数格式"; $.validator.messages.number = "请输入正确的小数格式"; //重构验证规则 $.validator.addClassRules({ "d&required": { required: true, digits: true }, "n&required": { required: true, number: true } }); //输入大小限制 $("input[type='text'].required").prop("maxlength", 50); $("input[type='text']").filter('.len500').prop("maxlength", 500); $("input[type='text']").filter('.n\\&required').prop("maxlength", 15); $("input[type='text']").filter('.d\\&required').prop("maxlength", 9); //$("textarea").prop("maxlength", 500); } } //相同name 验证 if ($.validator) { $.validator.prototype.elements = function () { var validator = this, rulesCache = {}; return $([]).add(this.currentForm.elements) .filter(":input") .not(":submit, :reset, :image, [disabled]") .not(this.settings.ignore) .filter(function () { var elementIdentification = this.id || this.name; !elementIdentification && validator.settings.debug && window.console && console.error("%o has no id nor name assigned", this); if (elementIdentification in rulesCache || !validator.objectLength($(this).rules())) return false; rulesCache[elementIdentification] = true; return true; }); }; } $(function () { jQuery.validator.addMethod("isMobile", function (value, element) { var regex = /^1[3-9]{1}\d{9}$/; return this.optional(element) || (regex.test(value)); }, "请输入正确的手机号码"); })