ks-validation_d.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. $(document.body).validatable();
  17. });
  18. jQuery.fn.extend({
  19. validatable : function() {
  20. $("input.datepicker", $(this)).each(function(){
  21. var input = this;
  22. var pattern = typeof(input.format) != 'undefined' ? input.pattern : 'yyyy-MM-dd';
  23. var offset = $(this).offset();
  24. $(this).attr('pattern', pattern).css("imeMode", "disabled").click(function(e) {
  25. var pos = $(this).offset();
  26. if (pos.left + $(this).width() - e.clientX < 18) {
  27. popCalendar(this, pattern, false);
  28. }
  29. });
  30. });
  31. $("input.numberbox", $(this)).keypress(function(evt){
  32. var key = evt.which;
  33. if (key != 45 && (key != 46 || $(this).hasClass("integer")) && key != 8 && key != 37 && key != 39 && (key < 48 || key > 57)) {
  34. evt.preventDefault();
  35. } else if (key == 46 && this.value.indexOf(".") != -1) {
  36. evt.preventDefault();
  37. }
  38. }).keyup(function(evt) {
  39. var ctrl = evt.ctrlKey;
  40. if (ctrl && evt.keyCode == 86) {
  41. if (isNaN(this.value)) this.value = '';
  42. }
  43. });
  44. $("input.required, input.datepicker, input.numberbox, select.required, textarea.required", $(this)).filter(":visible").each(function(){
  45. $(this).blur(function(){
  46. if (this.getAttribute("hcksid") == null) {
  47. this.setAttribute("hcksid", uuid());
  48. }
  49. var message = null;
  50. if ($(this).hasClass('required') && $.trim(this.value) == '') {
  51. message = __MESSAGE_REQUIRED;
  52. } else if ($(this).hasClass('datepicker')) {
  53. if (!validateDate(this.value, $(this).attr('pattern'))) {
  54. message = __MESSAGE_INVALID_DATE;
  55. }
  56. else {
  57. var matchResult = this.className.match(/(before|after)(#\w+)?/);
  58. if (matchResult != null && matchResult.length > 1) {
  59. var otherDate = __TODAY;
  60. if (matchResult.length > 2 && matchResult[2]) {
  61. var otherDp = document.getElementById(matchResult[2].substr(1));
  62. if (otherDp != null && otherDp.value.length > 0) {
  63. var otherDateArr = extractDate(otherDp.value, $(otherDp).attr('pattern'));
  64. otherDate = new Date(otherDateArr[0], otherDateArr[1], otherDateArr[2]);
  65. }
  66. }
  67. var thisDateArr = extractDate(this.value, $(this).attr('pattern'));
  68. var thisDate = new Date(thisDateArr[0], thisDateArr[1], thisDateArr[2]);
  69. if (thisDate.getTime() > otherDate.getTime() && matchResult[1] == 'before') {
  70. message = __MESSAGE_DATE_AFTER + otherDate.format();
  71. } else if (thisDate.getTime() < otherDate.getTime() && matchResult[1] == 'after') {
  72. message = __MESSAGE_DATE_BEFORE + otherDate.format();
  73. }
  74. }
  75. }
  76. } else if (this.value.length > 0 && $(this).hasClass('numberbox')) {
  77. if (isNaN(this.value) || this.value.match(/^\s+|\s+$/) != null) {
  78. message = __MESSAGE_INVALID_NUM;
  79. } else {
  80. var regx = /(?:^|\s)(gt|lt|ge|le)(-?\d+(?:\.\d+)?)/g;
  81. var mms = this.className.match(regx);
  82. if (mms != null) {
  83. for (var i = 0; i < mms.length; i++) {
  84. var r = /(?:^|\s)(gt|lt|ge|le)(-?\d+(?:\.\d+)?)/g;
  85. var matches = r.exec(mms[i]);
  86. if (matches != null && matches.length > 1) {
  87. var num = parseFloat(matches[2]);
  88. var thisnum = parseFloat(this.value);
  89. if (matches[1] == "gt" && (isNaN(thisnum) || thisnum <= num)) {
  90. message = __MESSAGE_GT + num;
  91. } else if (matches[1] == "lt" && (isNaN(thisnum) || thisnum >= num)) {
  92. message = __MESSAGE_LT + num;
  93. } else if (matches[1] == "ge" && (isNaN(thisnum) || thisnum < num)) {
  94. message = __MESSAGE_GE + num;
  95. } else if (matches[1] == "le" && (isNaN(thisnum) || thisnum > num)) {
  96. message = __MESSAGE_LE + num;
  97. }
  98. if (message != null) break;
  99. }
  100. }
  101. }
  102. }
  103. }
  104. if (message != null) {
  105. $(this).addClass("x-form-invalid");
  106. var icons = $("#v" + this.getAttribute("hcksid"));
  107. message = message.replace(/"\{0\}"/g, this.title);
  108. alert(message);
  109. } else {
  110. $(this).removeClass("x-form-invalid");
  111. $("#v" + this.getAttribute("hcksid")).remove();
  112. }
  113. });
  114. });
  115. $("form", $(this)).submit(function() {
  116. if ($(this).attr("validate") == false) {
  117. return true;
  118. }
  119. var r = $(this).validate();
  120. if (r == false) {
  121. alert("表单填写有误,请检查带有红色叹号标记的表单");
  122. }
  123. if (r) {
  124. $("input[type=button]", this).each(function() {
  125. if (this.className.indexOf('enable') == -1) {
  126. this.disabled = true;
  127. }
  128. });
  129. }
  130. return r;
  131. });
  132. return $(this);
  133. },
  134. validate : function() {
  135. var valid = true;
  136. if ($(this).attr("validate") != "false") {
  137. $("input.required, input.datepicker, input.numberbox, select.required, textarea.required", this).filter(
  138. function() {
  139. return $(this).is(":visible") && $(this).parents(":hidden").length == 0;
  140. }
  141. ).each( function() {
  142. $(this).blur();
  143. if (valid) {
  144. valid = !$(this).hasClass("x-form-invalid");
  145. }
  146. });
  147. }
  148. return valid;
  149. },
  150. revertValidation : function() {
  151. $(":text.x-form-invalid, select.x-form-invalid", this).each( function() {
  152. $(this).removeClass("x-form-invalid");
  153. $("#v" + this.getAttribute("hcksid")).remove();
  154. });
  155. return $(this);
  156. }
  157. });