ks-validation.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. var __NOW_UID = new Date().getTime();
  2. /*------------------------------------------*/
  3. var __MESSAGE_REQUIRED = '"{0}" 不能为空,必须填写!';
  4. var __MESSAGE_INVALID_DATE = '"{0}" 日期格式不正确!';
  5. var __MESSAGE_DATE_BEFORE = '"{0}" 不能早于';
  6. var __MESSAGE_DATE_AFTER = '"{0}" 不能晚于';
  7. var __MESSAGE_INVALID_NUM = '"{0}" 数字格式不正确!';
  8. var __MESSAGE_GT = '"{0}" 必须大于';
  9. var __MESSAGE_LT = '"{0}" 必须小于';
  10. var __MESSAGE_GE = '"{0}" 不能小于';
  11. var __MESSAGE_LE = '"{0}" 不能大于';
  12. function uuid() {
  13. return "U" + __NOW_UID ++;
  14. }
  15. $(document).ready(function(){
  16. var actionMessage = new Array();
  17. var actionError = new Array();
  18. $(".actionMessage span").each(function(){
  19. actionMessage[actionMessage.length] = ($(this).html());
  20. });
  21. if (actionMessage.length > 0) {
  22. var div = $("<div class='actionMessage'></div>");
  23. var message = "";
  24. for (var i = 0; i < actionMessage.length; i ++) {
  25. message += actionMessage[i];
  26. if (i < actionMessage.length - 1) message += "<br/>";
  27. }
  28. div.html(message);
  29. $(".actionMessage span").parents("ul:first").after(div).remove();
  30. }
  31. $(".errorMessage span").each(function(){
  32. actionError[actionError.length] = ($(this).html());
  33. });
  34. if (actionError.length > 0) {
  35. var div = $("<div class='errorMessage'></div>");
  36. var message = "";
  37. for (var i = 0; i < actionError.length; i ++) {
  38. message += actionError[i];
  39. if (i < actionError.length - 1) message += "<br/>";
  40. }
  41. div.html(message);
  42. $(".errorMessage span").parents("ul:first").after(div).remove();
  43. }
  44. $(document.body).validatable();
  45. $(document).keydown(function(evt) {
  46. var target = evt.target.tagName;
  47. if (evt.which == 13 && target == 'INPUT' || target == 'SELECT') {
  48. if (evt.target.type == 'button') {
  49. if (typeof(evt.target.onclick) == 'function') {
  50. evt.target.onclick.call(evt.target);
  51. } else {
  52. $(evt.target).trigger('click');
  53. }
  54. }
  55. if($.browser.msie) evt.keyCode = 9;
  56. }
  57. });
  58. });
  59. jQuery.fn.extend({
  60. validatable : function() {
  61. $("input.datepicker", $(this)).each(function(){
  62. var input = this;
  63. var pattern = typeof(input.format) != 'undefined' ? input.pattern : 'yyyy-MM-dd';
  64. var offset = $(this).offset();
  65. $(this).attr('pattern', pattern).css("imeMode", "disabled").click(function(e) {
  66. var pos = $(this).offset();
  67. if (pos.left + $(this).width() - e.clientX < 18) {
  68. popCalendar(this, pattern, false);
  69. }
  70. });
  71. });
  72. $("input.numberbox", $(this)).keypress(function(evt){
  73. var key = evt.which;
  74. if (key != 45 && (key != 46 || $(this).hasClass("integer")) && key != 8 && key != 37 && key != 39 && (key < 48 || key > 57)) {
  75. evt.preventDefault();
  76. } else if (key == 46 && this.value.indexOf(".") != -1) {
  77. evt.preventDefault();
  78. }
  79. }).keyup(function(evt) {
  80. var ctrl = evt.ctrlKey;
  81. if (ctrl && evt.keyCode == 86) {
  82. if (isNaN(this.value)) this.value = '';
  83. }
  84. });
  85. $("input.required, input.datepicker, input.numberbox, select.required, textarea.required", $(this)).filter(":visible").each(function(){
  86. $(this).blur(function(){
  87. if (this.getAttribute("hcksid") == null) {
  88. this.setAttribute("hcksid", uuid());
  89. }
  90. var message = null;
  91. if ($(this).hasClass('required') && $.trim(this.value) == '') {
  92. message = __MESSAGE_REQUIRED;
  93. } else if ($(this).hasClass('datepicker')) {
  94. if (!validateDate(this.value, $(this).attr('pattern'))) {
  95. message = __MESSAGE_INVALID_DATE;
  96. }
  97. else {
  98. var matchResult = this.className.match(/(before|after)(#\w+)?/);
  99. if (matchResult != null && matchResult.length > 1) {
  100. var otherDate = __TODAY;
  101. if (matchResult.length > 2 && matchResult[2]) {
  102. var otherDp = document.getElementById(matchResult[2].substr(1));
  103. if (otherDp != null && otherDp.value.length > 0) {
  104. var otherDateArr = extractDate(otherDp.value, $(otherDp).attr('pattern'));
  105. otherDate = new Date(otherDateArr[0], otherDateArr[1], otherDateArr[2]);
  106. }
  107. }
  108. var thisDateArr = extractDate(this.value, $(this).attr('pattern'));
  109. var thisDate = new Date(thisDateArr[0], thisDateArr[1], thisDateArr[2]);
  110. if (thisDate.getTime() > otherDate.getTime() && matchResult[1] == 'before') {
  111. message = __MESSAGE_DATE_AFTER + otherDate.format();
  112. } else if (thisDate.getTime() < otherDate.getTime() && matchResult[1] == 'after') {
  113. message = __MESSAGE_DATE_BEFORE + otherDate.format();
  114. }
  115. }
  116. }
  117. } else if (this.value.length > 0 && $(this).hasClass('numberbox')) {
  118. if (isNaN(this.value) || this.value.match(/^\s+|\s+$/) != null) {
  119. message = __MESSAGE_INVALID_NUM;
  120. } else {
  121. var regx = /(?:^|\s)(gt|lt|ge|le)(-?\d+(?:\.\d+)?)/g;
  122. var mms = this.className.match(regx);
  123. if (mms != null) {
  124. for (var i = 0; i < mms.length; i++) {
  125. var r = /(?:^|\s)(gt|lt|ge|le)(-?\d+(?:\.\d+)?)/g;
  126. var matches = r.exec(mms[i]);
  127. if (matches != null && matches.length > 1) {
  128. var num = parseFloat(matches[2]);
  129. var thisnum = parseFloat(this.value);
  130. if (matches[1] == "gt" && (isNaN(thisnum) || thisnum <= num)) {
  131. message = __MESSAGE_GT + num;
  132. } else if (matches[1] == "lt" && (isNaN(thisnum) || thisnum >= num)) {
  133. message = __MESSAGE_LT + num;
  134. } else if (matches[1] == "ge" && (isNaN(thisnum) || thisnum < num)) {
  135. message = __MESSAGE_GE + num;
  136. } else if (matches[1] == "le" && (isNaN(thisnum) || thisnum > num)) {
  137. message = __MESSAGE_LE + num;
  138. }
  139. if (message != null) break;
  140. }
  141. }
  142. }
  143. }
  144. }
  145. if (message != null) {
  146. $(this).addClass("x-form-invalid");
  147. var icons = $("#v" + this.getAttribute("hcksid"));
  148. message = message.replace(/"\{0\}"/g, this.title);
  149. if (icons.length == 0) {
  150. var offset = $(this).offset();
  151. var prevSib = $(this).next("img");
  152. var invalidIcon = $("<img id='v" + this.getAttribute("hcksid") + "'/>").attr("src", __BLANK_IMAGE).addClass('x-form-invalid-icon')
  153. .attr("title", message);
  154. if (prevSib.length == 0) {
  155. prevSib = $(this);
  156. } else {
  157. invalidIcon.css("left", "-16px");
  158. }
  159. prevSib.after(invalidIcon);
  160. } else {
  161. icons.attr("title", message);
  162. }
  163. } else {
  164. $(this).removeClass("x-form-invalid");
  165. $("#v" + this.getAttribute("hcksid")).remove();
  166. }
  167. });
  168. });
  169. $("form", $(this)).submit(function() {
  170. if ($(this).attr("validate") == false) {
  171. return true;
  172. }
  173. var r = $(this).validate();
  174. if (r == false) {
  175. alert("表单填写有误,请检查带有红色叹号标记的表单");
  176. // var errMsgs = [];
  177. // $(":text.x-form-invalid, select.x-form-invalid", this).each( function() {
  178. // var erricon = $("#v" + this.getAttribute("hcksid"));
  179. // if (erricon.length > 0) errMsgs.push(erricon.attr("title"));
  180. // });
  181. // if (errMsgs.length > 0) {
  182. // var errPanel = $("#HCKS_VALIDATE_ERROR_PANEL");
  183. // if (errPanel.length == 0) {
  184. // errPanel = $("<div id='HCKS_VALIDATE_ERROR_PANEL'></div>");
  185. // errPanel.append("<div class='errreporthead'>表单填写有误,请检查带有红色叹号标记的表单</div>");
  186. // errPanel.append("<div class='errreportbody'></div>");
  187. // var btnDiv = $("<div class='errreportbtn'></div>");
  188. // var errok = $("<input type='button' class='button' value=' 确定 '/>").click(function() {
  189. // $("div.errreportbody", errPanel).html("");
  190. // errPanel.unblockUI();
  191. // });
  192. // errPanel.append(btnDiv.append(errok));
  193. // $(document.body).append(errPanel);
  194. // }
  195. // var msgs = "";
  196. // for (var i = 0; i < errMsgs.length; i++) {
  197. // if (i > 0) msgs += "<br/>";
  198. // msgs += (i + 1) + "、" + errMsgs[i];
  199. // }
  200. // $("div.errreportbody", errPanel).html(msgs);
  201. // errPanel.blockUI();
  202. // }
  203. }
  204. if (r) {
  205. $("input[type=button]", this).each(function() {
  206. if (this.className.indexOf('enable') == -1) {
  207. this.disabled = true;
  208. }
  209. });
  210. }
  211. return r;
  212. });
  213. return $(this);
  214. },
  215. validate : function() {
  216. var valid = true;
  217. if ($(this).attr("validate") != "false") {
  218. $("input.required, input.datepicker, input.numberbox, select.required, textarea.required", this).filter(
  219. function() {
  220. return $(this).is(":visible") && $(this).parents(":hidden").length == 0;
  221. }
  222. ).each( function() {
  223. $(this).blur();
  224. if (valid) {
  225. valid = !$(this).hasClass("x-form-invalid");
  226. }
  227. });
  228. }
  229. return valid;
  230. },
  231. revertValidation : function() {
  232. $(":text.x-form-invalid, select.x-form-invalid", this).each( function() {
  233. $(this).removeClass("x-form-invalid");
  234. $("#v" + this.getAttribute("hcksid")).remove();
  235. });
  236. return $(this);
  237. }
  238. });