jquery.validate.easyui.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. (function ($) {
  2. //修改MVC Model验证为EasyUI模式
  3. $.fn.makeValidationEasyUI = function (options) {
  4. var settings = {
  5. showTriangle: true,
  6. promptPosition: "topRight"
  7. };
  8. $.extend(settings, options);
  9. //把验证标记切换到easyUI生成的input
  10. // $("[comboname]").each(function (i) {
  11. // var $this = $(this);
  12. // var $easyUI = $("[name='" + $this.attr("comboname") + "']");
  13. // if ($this.attr("data-val")) {
  14. // for (var j = this.attributes.length - 1; j >= 0; j--) {
  15. // if (this.attributes[j].name.indexOf("data-val") >= 0) {
  16. // $easyUI.attr(this.attributes[j].name, this.attributes[j].value);
  17. // $this.removeAttr(this.attributes[j].name);
  18. // }
  19. // }
  20. // $this.removeAttr("data-val");
  21. // }
  22. // });
  23. return this.each(function () {
  24. var $form = $(this);
  25. //#region message prompt functions
  26. /*
  27. * Inline Form Validation Engine 1.3.9.5, jQuery plugin
  28. *
  29. * Copyright(c) 2009, Cedric Dugas
  30. * http://www.position-relative.net
  31. *
  32. * Form validation engine which allow custom regex rules to be added.
  33. * Licenced under the MIT Licence
  34. * Modified by Jeffrey Lee, http://blog.darkthread.net, to support ASP.NET MVC 3
  35. * Modified by wufengshuo to EasyUIValidate type
  36. */
  37. //新增提示对象
  38. function showtip(event) {//input
  39. movevalidate_tip($(event.currentTarget), event.data.name);
  40. $("div[data-valeasyui-for='" + event.data.name + "']").show();
  41. }
  42. function hidetip(event) {//input
  43. $("div[data-valeasyui-for='" + event.data.name + "']").hide();
  44. }
  45. function addvalidate_tip(caller, promptText, type, ajaxed) {
  46. var bowintip = $("div[data-valeasyui-for='" + caller.name + "']");
  47. if (bowintip.length == 0) {
  48. bowintip = $("<div class=\"validatebox-tip\">" + "<span class=\"validatebox-tip-content\">" + "</span>" + "<span class=\"validatebox-tip-pointer\">" + "</span>" + "</div>").appendTo("body");
  49. bowintip.attr("data-valeasyui-for", caller.name);
  50. }
  51. bowintip.find(".validatebox-tip-content").html(promptText);
  52. return bowintip;
  53. }
  54. function movevalidate_tip($caller, name) {
  55. var bowintip = $("div[data-valeasyui-for='" + name + "']");
  56. if (bowintip) {
  57. var tip_pointer = bowintip.find(".validatebox-tip-pointer");
  58. var tip_content = bowintip.find(".validatebox-tip-content");
  59. bowintip.show();
  60. bowintip.css("top", $caller.offset().top - (tip_content._outerHeight() - $caller._outerHeight()) / 2);
  61. // if (_3ae.options.tipPosition == "left") {
  62. //bowintip.css("left", box.offset().left - bowintip._outerWidth());
  63. //bowintip.addClass("validatebox-tip-left");
  64. // } else {
  65. bowintip.css("left", $caller.offset().left + $caller._outerWidth());
  66. bowintip.removeClass("validatebox-tip-left");
  67. // }
  68. tip_pointer.css("top", (tip_content._outerHeight() - tip_pointer._outerHeight()) / 2);
  69. bowintip.hide();
  70. }
  71. };
  72. function removevalidate_tip(caller) {
  73. var $caller = findShowTipInput(caller);
  74. var bowintip = $("div[data-valeasyui-for='" + caller.name + "']");
  75. if (bowintip) {
  76. $caller.removeClass("ValidateEntity-invalid");
  77. $caller.removeClass("input-validation-error");
  78. $caller.off("mouseenter", showtip);
  79. $caller.off("mouseleave", hidetip);
  80. bowintip.remove();
  81. }
  82. };
  83. function onEasyUIChange(newvalue, oldvalue) {
  84. }
  85. function showInvalidPrompt(caller, message, error) {
  86. var $caller = findShowTipInput(caller);
  87. //function () { showtip(caller); }, function () { hidetip(caller); }
  88. $caller.on("mouseenter", caller, showtip);
  89. $caller.on("mouseleave", caller, hidetip);
  90. $caller.addClass("ValidateEntity-invalid");
  91. if ($caller.parent().hasClass("easyui-combogridX")) {
  92. $caller.addClass("input-validation-error");
  93. var sourceobj = $("[comboname='" + caller.name + "']");
  94. sourceobj.combogridX({ onChange: function () {
  95. var validator = $form.validate();
  96. var vitem = sourceobj.find(".combogridX-value")[0];
  97. if (vitem.name in validator.submitted || !validator.optional(vitem)) {
  98. if (validator.element(vitem)) {
  99. $caller.removeClass("ValidateEntity-invalid");
  100. $caller.removeClass("input-validation-error");
  101. $caller.off("mouseenter", showtip);
  102. $caller.off("mouseleave", hidetip);
  103. }
  104. else {
  105. $caller.on("mouseenter", caller, showtip);
  106. $caller.on("mouseleave", caller, hidetip);
  107. $caller.addClass("ValidateEntity-invalid");
  108. $caller.addClass("input-validation-error");
  109. }
  110. }
  111. }
  112. });
  113. }
  114. if ($caller.is(".combo-text") && !$caller.parent().hasClass("easyui-combogridX")) {
  115. $caller.addClass("input-validation-error");
  116. var sourceobj = $("[comboname='" + caller.name + "']");
  117. sourceobj.combo({ onChange: function (newvalue, oldvalue) {
  118. var $this = $(this);
  119. $this.val(newvalue);
  120. //$this.attr("name", $this.attr("comboname"));
  121. var vitem = $("[name='" + $this.attr("comboname") + "']")[0];
  122. var validator = $form.validate();
  123. if (vitem.name in validator.submitted || !validator.optional(vitem)) {
  124. if (validator.element(vitem)) {
  125. $caller.removeClass("ValidateEntity-invalid");
  126. $caller.removeClass("input-validation-error");
  127. $caller.off("mouseenter", showtip);
  128. $caller.off("mouseleave", hidetip);
  129. }
  130. else {
  131. $caller.on("mouseenter", caller, showtip);
  132. $caller.on("mouseleave", caller, hidetip);
  133. $caller.addClass("ValidateEntity-invalid");
  134. $caller.addClass("input-validation-error");
  135. }
  136. }
  137. //$this.attr("name", "");
  138. }
  139. });
  140. //$caller.focus();
  141. }
  142. var validate_tip = addvalidate_tip(caller, message, "error", false);
  143. movevalidate_tip($caller, caller.name);
  144. var validator = $form.validate();
  145. // if (validator.errorList[validator.errorList.length - 1].element.name == caller.name) {
  146. // for (var i = validator.errorList.length - 1; i >= 0; i--) {
  147. // if (validator.errorList[i].element.type != "hidden") {
  148. // validator.errorList[i].element.focus();
  149. // break;
  150. // }
  151. // }
  152. // }
  153. return validate_tip;
  154. }
  155. function hideInvalidPrompt(caller) {
  156. removevalidate_tip(caller);
  157. }
  158. //#endregion
  159. function findShowTipInput(caller) {
  160. var $caller = $(caller);
  161. var $ShowTipcaller;
  162. if ($caller.is(".combo-value"))
  163. $ShowTipcaller = $caller.parent().find(".combo-text");
  164. else if ($caller.is(".combogridX-value"))
  165. $ShowTipcaller = $caller.parent().find(".combo-text");
  166. else
  167. $ShowTipcaller = $caller;
  168. return $ShowTipcaller;
  169. }
  170. function custErrorPlacement(error, inputElement) { // 'this' is the form element
  171. var msg = error.text();
  172. if (msg.length > 1 && !inputElement.hasClass("datebox"))
  173. error.data("inv_msg_prompt", showInvalidPrompt(inputElement[0], msg, error));
  174. else
  175. hideInvalidPrompt(inputElement[0]);
  176. }
  177. function custSuccess(error) { // 'this' is the form element
  178. var p = error.data("inv_msg_prompt");
  179. if (p) p.remove();
  180. }
  181. //#region change errorPlacement
  182. var valdSettings = $form.data("validator").settings;
  183. // $form.find("input[name=" + value.name + "]").each(function (index, value) {
  184. // if ($form.data("validator").settings.rules[value.name]) {
  185. //
  186. // }
  187. // });
  188. valdSettings.errorElement = "div";
  189. valdSettings.errorClass = "input-validation-error ValidateEntity-invalid";
  190. valdSettings.errorPlacement = $.proxy(custErrorPlacement, this);
  191. valdSettings.success = $.proxy(custSuccess, this);
  192. valdSettings.ignore = "";
  193. $form.validate(valdSettings);
  194. //#endregion
  195. });
  196. }
  197. })(jQuery);
  198. //----------------------------------------------
  199. // Begin MVC Validate Extend
  200. //-----------------------------------------------
  201. (function () {
  202. var $jQval = $.validator;
  203. var adapters = $.validator.unobtrusive.adapters;
  204. function getModelPrefix(fieldName) {
  205. return fieldName.substr(0, fieldName.lastIndexOf(".") + 1);
  206. }
  207. function appendModelPrefix(value, prefix) {
  208. if (value.indexOf("*.") === 0) {
  209. value = value.replace("*.", prefix);
  210. }
  211. return value;
  212. }
  213. function setValidationValues(options, ruleName, value) {
  214. options.rules[ruleName] = value;
  215. if (options.message) {
  216. options.messages[ruleName] = options.message;
  217. }
  218. }
  219. $jQval.addMethod('notlessthan', function (value, element, otherproperty) {
  220. if (value) {
  221. try {
  222. var prefix = getModelPrefix(element.name);
  223. var fullOtherName = appendModelPrefix(otherproperty, prefix);
  224. var other = $('input[name="' + fullOtherName + '"]').val();
  225. var regEx = new RegExp("\\-","gi");
  226. other=other.replace(regEx,"/");
  227. var targetDate = new Date(other);
  228. value=value.replace(regEx,"/");
  229. var dt = new Date(value);
  230. return dt > targetDate;
  231. } catch (e) {
  232. return false;
  233. }
  234. }
  235. return true;
  236. });
  237. adapters.addSingleVal('notlessthan', 'otherproperty');
  238. $jQval.addMethod('rangeifidsin', function (value, element, params) {
  239. if (value) {
  240. try {
  241. var min = params.min,
  242. max = params.max,
  243. ids = params.ids,
  244. template = params.template,
  245. otherproperty = params.otherproperty;
  246. var otherVal = $('input[name="' + otherproperty + '"]').val();
  247. otherVal = template.replace('{0}', otherVal).toLowerCase();
  248. ids = ids.toLowerCase();
  249. value=parseInt(value);
  250. min=parseInt(min);
  251. max=parseInt(max);
  252. if (ids.indexOf(otherVal) >= 0) {
  253. return value >= min && value <= max;
  254. }
  255. } catch (e) {
  256. return false;
  257. }
  258. }
  259. return true;
  260. });
  261. adapters.RangeIfIdsIn = function (adapterName, otherproperty, ids, minimum, maximum, template) {
  262. return adapters.add(adapterName, [minimum, maximum, otherproperty, ids, template], function (options) {
  263. var min = options.params.minimum,
  264. max = options.params.maximum,
  265. ids = options.params.ids,
  266. template = options.params.template,
  267. otherproperty = options.params.otherproperty;
  268. var prefix = getModelPrefix(options.element.name);
  269. var fullOtherName = appendModelPrefix(otherproperty, prefix);
  270. if (min && max && ids && otherproperty) {
  271. setValidationValues(options
  272. , adapterName
  273. , {
  274. 'min': min, 'max': max, 'ids': ids, 'otherproperty': fullOtherName, 'template': template
  275. });
  276. }
  277. });
  278. };
  279. adapters.RangeIfIdsIn('rangeifidsin', 'otherproperty', 'ids', 'minimum', 'maximum', 'template');
  280. })();
  281. $(function () {
  282. if ($.fn.makeValidationEasyUI) {
  283. //$(".formitem").parents("form").makeValidationEasyUI();
  284. $("form").makeValidationEasyUI();
  285. }
  286. $('form .easyui-datebox[validtype="daterequired"],form .easyui-datetimebox[validtype="daterequired"]').each(function () {
  287. var message = $(this).attr("invalidmessage");
  288. $(this).validatebox("removeForCombobox");
  289. $(this).validatebox("reduceForCombobox");
  290. //$(this).focus(function () { $(this).validatebox({ required: false, validType: 'daterequired', invalidMessage: message }); });
  291. });
  292. $("form").bind("submit.form", function () {
  293. $(this).find('.easyui-datebox[validtype="daterequired"],.easyui-datetimebox[validtype="daterequired"]').each(function () {
  294. var message = $(this).attr("invalidmessage");
  295. $(this).validatebox({ required: false, validType: 'daterequired', invalidMessage: message });
  296. });
  297. });
  298. //$('form .easyui-datebox[validtype="daterequired"],form .easyui-datetimebox[validtype="daterequired"]').validatebox('disableValidation')
  299. //.focus(function () { $(this).validatebox('enableValidation'); })
  300. //.blur(function () { $(this).validatebox('validate') });
  301. });
  302. //----------------------------------------------
  303. // End MVC Validate Extend
  304. //-----------------------------------------------