jquery.validate.easyui.js 15 KB

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