jquery.validate.inline.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. (function ($) {
  2. //Change <span> validation message display to top right corner style prompt
  3. $.fn.makeValidationInline = function (options) {
  4. var settings = {
  5. showTriangle: true,
  6. promptPosition: "topRight"
  7. };
  8. $.extend(settings, options);
  9. return this.each(function () {
  10. var $form = $(this);
  11. //#region message prompt functions
  12. /*
  13. * Inline Form Validation Engine 1.3.9.5, jQuery plugin
  14. *
  15. * Copyright(c) 2009, Cedric Dugas
  16. * http://www.position-relative.net
  17. *
  18. * Form validation engine which allow custom regex rules to be added.
  19. * Licenced under the MIT Licence
  20. * Modified by Jeffrey Lee, http://blog.darkthread.net, to support ASP.NET MVC 3
  21. */
  22. function buildPrompt(caller, promptText, type, ajaxed) { // ERROR PROMPT CREATION AND DISPLAY WHEN AN ERROR OCCUR
  23. var $divFormError = $("<div />");
  24. var $formErrorContent = $("<div class='formErrorContent' />");
  25. $formErrorContent.html(promptText);
  26. var $caller = $(caller);
  27. if ($caller.is(".combo-value")) {
  28. $caller = $caller.parent().find(".combo-text");
  29. }
  30. $divFormError.addClass("formError");
  31. if (type == "pass") { $divFormError.addClass("greenPopup") }
  32. if (type == "load") { $divFormError.addClass("blackPopup") }
  33. if (ajaxed) { $divFormError.addClass("ajaxed") }
  34. $divFormError.addClass(caller.name)
  35. .append($formErrorContent).appendTo("body");
  36. if (settings.showTriangle != false) { // NO TRIANGLE ON MAX CHECKBOX AND RADIO
  37. var $arrow = $("<div class='formErrorArrow' />");
  38. $divFormError.append($arrow)
  39. if (settings.promptPosition == "bottomLeft" || settings.promptPosition == "bottomRight") {
  40. $arrow.addClass("formErrorArrowBottom")
  41. $arrow.html('<div class="line1"><!-- --></div><div class="line2"><!-- --></div><div class="line3"><!-- --></div><div class="line4"><!-- --></div><div class="line5"><!-- --></div><div class="line6"><!-- --></div><div class="line7"><!-- --></div><div class="line8"><!-- --></div><div class="line9"><!-- --></div><div class="line10"><!-- --></div>');
  42. }
  43. if (settings.promptPosition == "topLeft" || settings.promptPosition == "topRight") {
  44. $divFormError.append($arrow)
  45. $arrow.html('<div class="line10"><!-- --></div><div class="line9"><!-- --></div><div class="line8"><!-- --></div><div class="line7"><!-- --></div><div class="line6"><!-- --></div><div class="line5"><!-- --></div><div class="line4"><!-- --></div><div class="line3"><!-- --></div><div class="line2"><!-- --></div><div class="line1"><!-- --></div>');
  46. }
  47. }
  48. callerTopPosition = $caller.offset().top;
  49. callerleftPosition = $caller.offset().left;
  50. callerWidth = $caller.width()
  51. inputHeight = $divFormError.height()
  52. /* POSITIONNING */
  53. if (settings.promptPosition == "topRight") { callerleftPosition += callerWidth - 30; callerTopPosition += -inputHeight - 10; }
  54. if (settings.promptPosition == "topLeft") { callerTopPosition += -inputHeight - 10; }
  55. if (settings.promptPosition == "centerRight") { callerleftPosition += callerWidth + 13; }
  56. if (settings.promptPosition == "bottomLeft") {
  57. callerHeight = $caller.height();
  58. callerleftPosition = callerleftPosition;
  59. callerTopPosition = callerTopPosition + callerHeight + 15;
  60. }
  61. if (settings.promptPosition == "bottomRight") {
  62. callerHeight = $caller.height();
  63. callerleftPosition += callerWidth - 30;
  64. callerTopPosition += callerHeight + 15;
  65. }
  66. $divFormError.css({
  67. top: callerTopPosition,
  68. left: callerleftPosition,
  69. opacity: 0
  70. })
  71. return $divFormError.animate({ "opacity": 0.8 }, function () { return true; });
  72. }
  73. function updatePromptText(caller, promptText, type, ajaxed) { // UPDATE TEXT ERROR IF AN ERROR IS ALREADY DISPLAYED
  74. var $caller = $(caller);
  75. //支持easyUI下拉单,时间控件
  76. if ($caller.is(".combo-value")) {
  77. $caller = $caller.parent().find(".combo-text");
  78. }
  79. var $updateThisPrompt = $("div." + caller.name);
  80. if ($updateThisPrompt.length == 0)
  81. return buildPrompt(caller, promptText, type, ajaxed);
  82. (type == "pass") ? $updateThisPrompt.addClass("greenPopup") : $updateThisPrompt.removeClass("greenPopup");
  83. (type == "load") ? $updateThisPrompt.addClass("blackPopup") : $updateThisPrompt.removeClass("blackPopup");
  84. (ajaxed) ? $updateThisPrompt.addClass("ajaxed") : $updateThisPrompt.removeClass("ajaxed");
  85. $updateThisPrompt.find(".formErrorContent").html(promptText);
  86. callerTopPosition = $caller.offset().top;
  87. inputHeight = $updateThisPrompt.height()
  88. if (settings.promptPosition == "bottomLeft" || settings.promptPosition == "bottomRight") {
  89. callerHeight = $caller.height()
  90. callerTopPosition = callerTopPosition + callerHeight + 15
  91. }
  92. if (settings.promptPosition == "centerRight") { callerleftPosition += callerWidth + 13; }
  93. if (settings.promptPosition == "topLeft" || settings.promptPosition == "topRight") {
  94. callerTopPosition = callerTopPosition - inputHeight - 10
  95. }
  96. $updateThisPrompt.animate({
  97. top: callerTopPosition
  98. });
  99. return $updateThisPrompt;
  100. }
  101. function showInvalidPrompt(caller, message) {
  102. updatePromptText(caller, message, "error", false);
  103. }
  104. function hideInvalidPrompt(caller) {
  105. $("div." + caller.name).remove();
  106. }
  107. //#endregion
  108. function custErrorPlacement(error, inputElement) { // 'this' is the form element
  109. var container = $(this).find("[data-valmsg-for='" + inputElement[0].name + "']");
  110. if (container.length == 0) {
  111. container = $("<span data-valmsg-for='" + inputElement[0].name + "' />");
  112. $(inputElement).after(container);
  113. }
  114. var msg = error.text();
  115. if (msg.length > 1)
  116. error.data("inv_msg_prompt", showInvalidPrompt(inputElement[0], msg));
  117. else
  118. hideInvalidPrompt(inputElement[0]);
  119. }
  120. function custSuccess(error) { // 'this' is the form element
  121. var p = error.data("inv_msg_prompt");
  122. if (p) p.remove();
  123. }
  124. //#region change errorPlacement
  125. var valdSettings = $form.data("validator").settings;
  126. valdSettings.errorElement = "li";
  127. valdSettings.errorPlacement = $.proxy(custErrorPlacement, this);
  128. valdSettings.success = $.proxy(custSuccess, this);
  129. $form.validate(valdSettings);
  130. //#endregion
  131. });
  132. }
  133. })(jQuery);