You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

formvalid.js 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. Jquery
  3. janchie 2010.1
  4. 1.02版
  5. */
  6. var validResult = {};
  7. var errorMsg = {};
  8. (function ($) {
  9. $.fn.extend({
  10. valid: function () {
  11. if (!$(this).is("form")) return;
  12. var items = $.isArray(arguments[0]) ? arguments[0] : [],
  13. isBindSubmit = typeof arguments[1] === "boolean" ? arguments[1] : true,
  14. isAlert = typeof arguments[2] === "boolean" ? arguments[2] : false,
  15. rule = {
  16. "eng": /^[A-Za-z]+$/,
  17. "chn": /^[\u0391-\uFFE5]+$/,
  18. "mail": /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/,
  19. "url": /^http[s]?:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/,
  20. "currency": /^\d+(\.\d+)?$/,
  21. "number": /^\d+$/,
  22. "int": /^[0-9]{1,30}$/,
  23. "double": /^[-\+]?\d+(\.\d+)?$/,
  24. "username": /^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){3,19}$/,
  25. "password": /^[\w\W]{6,20}$/,
  26. "safe": />|<|,|\[|\]|\{|\}|\?|\/|\+|=|\||\'|\\|\"|:|;|\~|\!|\@|\#|\*|\$|\%|\^|\&|\(|\)|`/i,
  27. "dbc": /[a-zA-Z0-9!@#¥%^&*()_+{}[]|:"';.,/?<>`~ ]/,
  28. "qq": /[1-9][0-9]{4,}/,
  29. "date": /^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$/,
  30. "year": /^(19|20)[0-9]{2}$/,
  31. "month": /^(0?[1-9]|1[0-2])$/,
  32. "day": /^((0?[1-9])|((1|2)[0-9])|30|31)$/,
  33. "hour": /^((0?[1-9])|((1|2)[0-3]))$/,
  34. "minute": /^((0?[1-9])|((1|5)[0-9]))$/,
  35. "second": /^((0?[1-9])|((1|5)[0-9]))$/,
  36. "mobile": /^((\(\d{2,3}\))|(\d{3}\-))?13\d{9}$/,
  37. "phone": /^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/,
  38. "zipcode": /^[1-9]\d{5}$/,
  39. "IDcard": /^((1[1-5])|(2[1-3])|(3[1-7])|(4[1-6])|(5[0-4])|(6[1-5])|71|(8[12])|91)\d{4}((19\d{2}(0[13-9]|1[012])(0[1-9]|[12]\d|30))|(19\d{2}(0[13578]|1[02])31)|(19\d{2}02(0[1-9]|1\d|2[0-8]))|(19([13579][26]|[2468][048]|0[48])0229))\d{3}(\d|X|x)?$/,
  40. "ip": /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/,
  41. "file": /^[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/,
  42. "image": /.+\.(jpg|gif|png|bmp)$/i,
  43. "word": /.+\.(doc|rtf|pdf)$/i,
  44. "port": function (port) {
  45. return (!isNaN(port) && port > 0 && port < 65536) ? true : false;
  46. },
  47. "eq": function (arg1, arg2) {
  48. return arg1 == arg2 ? true : false;
  49. },
  50. "gt": function (arg1, arg2) {
  51. return arg1 > arg2 ? true : false;
  52. },
  53. "gte": function (arg1, arg2) {
  54. return arg1 >= arg2 ? true : false;
  55. },
  56. "lt": function (arg1, arg2) {
  57. return arg1 < arg2 ? true : false;
  58. },
  59. "lte": function (arg1, arg2) {
  60. return arg1 <= arg2 ? true : false;
  61. }
  62. },
  63. msgSuffix = {
  64. "eng": "only english welcomed",
  65. "chn": "only chinese welcomed",
  66. "mail": "invalid email format",
  67. "url": "invalid url format",
  68. "currency": "invalid number format",
  69. "number": "only number welcomed",
  70. "int": "only integer welcomed",
  71. "double": "only float welcomed",
  72. "username": "invalid username format,4-20 characters",
  73. "password": "warning, you'd better use 6-20 characters",
  74. "safe": "forbidden special characters",
  75. "dbc": "forbidden full width characters",
  76. "qq": "invalid qq format",
  77. "date": "invalid date format",
  78. "year": "invalid year format",
  79. "month": "invalid month format",
  80. "day": "invalid day format",
  81. "hour": "invalid hour format",
  82. "minute": "invalid minute format",
  83. "second": "invalid second format",
  84. "mobile": "invalid mobile format",
  85. "phone": "invalid phone format",
  86. "zipcode": "invalid zipcode format",
  87. "IDcard": "invalid identity format",
  88. "ip": "invalid ip format",
  89. "port": "invalid port format",
  90. "file": "invalid file format",
  91. "image": "invalid image format",
  92. "word": "invalid word file format",
  93. "eq": "not equal",
  94. "gt": "no greater than",
  95. "gte": "no greater than or equal",
  96. "lt": "no smaller than",
  97. "lte": "no smaller than or equal"
  98. },
  99. msg = "", formObj = $(this), checkRet = true, isAll,
  100. tipname = function (namestr) {
  101. return "tip_" + namestr.replace(/([a-zA-Z0-9])/g, "-$1");
  102. },
  103. typeTest = function () {
  104. var result = true, args = arguments;
  105. if (rule.hasOwnProperty(args[0])) {
  106. var t = rule[args[0]], v = args[1];
  107. result = args.length > 2 ? t.apply(arguments, [].slice.call(args, 1)) : ($.isFunction(t) ? t(v) : t.test(v));
  108. }
  109. return result;
  110. },
  111. showError = function (fieldObj, filedName, warnInfo) {
  112. checkRet = false;
  113. var tipObj = $("#" + tipname(filedName));
  114. if (tipObj.length > 0) tipObj.remove();
  115. var tipPosition = fieldObj.next().length > 0 ? fieldObj.nextAll().eq(this.length - 1) : fieldObj.eq(this.length - 1);
  116. //tipPosition.after("<span class='tooltip' id='" + tipname(filedName) + "'> " + warnInfo + " </span>");
  117. validResult[filedName] = false;
  118. errorMsg[filedName] = warnInfo;
  119. if (isAlert && isAll) msg = warnInfo;
  120. },
  121. showRight = function (fieldObj, filedName) {
  122. var tipObj = $("#" + tipname(filedName));
  123. if (tipObj.length > 0) tipObj.remove();
  124. var tipPosition = fieldObj.next().length > 0 ? fieldObj.nextAll().eq(this.length - 1) : fieldObj.eq(this.length - 1);
  125. //tipPosition.after("<span class='tooltip' id='" + tipname(filedName) + "'>correct</span>");
  126. validResult[filedName] = true;
  127. },
  128. findTo = function (objName) {
  129. var find;
  130. $.each(items, function () {
  131. if (this.name == objName && this.simple) {
  132. find = this.simple;
  133. return false;
  134. }
  135. });
  136. if (!find) find = $("[name='" + objName + "']")[0].name;
  137. return find;
  138. },
  139. fieldCheck = function (item) {
  140. var i = item, field = $("[name='" + i.name + "']", formObj[0]);
  141. if (!field[0]) return;
  142. var warnMsg, fv = $.trim(field.val()), isRq = typeof i.require === "boolean" ? i.require : true;
  143. if (isRq && ((field.is(":radio") || field.is(":checkbox")) && !field.is(":checked"))) {
  144. warnMsg = i.message || "choice needed";
  145. showError(field, i.name, warnMsg);
  146. } else if (isRq && fv == "") {
  147. warnMsg = i.message || ( field.is("select") ? "choice needed" : "not none" );
  148. showError(field, i.name, warnMsg);
  149. } else if (fv != "") {
  150. if (i.min || i.max) {
  151. var len = fv.length, min = i.min || 0, max = i.max;
  152. warnMsg = i.message || (max ? "range" + min + "~" + max + "" : "min length" + min);
  153. if ((max && (len > max || len < min)) || (!max && len < min)) {
  154. showError(field, i.name, warnMsg);
  155. return;
  156. }
  157. }
  158. if (i.type) {
  159. var matchVal = i.to ? $.trim($("[name='" + i.to + "']").val()) : i.value;
  160. var matchRet = matchVal ? typeTest(i.type, fv, matchVal) : typeTest(i.type, fv);
  161. warnMsg = i.message || msgSuffix[i.type];
  162. if (matchVal) warnMsg += (i.to ? findTo(i.to) + "value" : i.value);
  163. if (!matchRet) showError(field, i.name, warnMsg);
  164. else showRight(field, i.name);
  165. } else {
  166. showRight(field, i.name);
  167. }
  168. } else if (isRq) {
  169. showRight(field, i.name);
  170. }
  171. },
  172. validate = function () {
  173. $.each(items, function () {
  174. isAll = true;
  175. fieldCheck(this);
  176. });
  177. if (isAlert && msg != "") {
  178. alert(msg);
  179. msg = "";
  180. }
  181. return checkRet;
  182. };
  183. $.each(items, function () {
  184. var field = $("[name='" + this.name + "']", formObj[0]);
  185. if (field.is(":hidden")) return;
  186. var obj = this, toCheck = function () {
  187. isAll = false;
  188. fieldCheck(obj);
  189. };
  190. if (field.is(":file") || field.is("select")) {
  191. field.change(toCheck);
  192. } else {
  193. field.blur(toCheck);
  194. }
  195. });
  196. if (isBindSubmit) {
  197. $(this).submit(validate);
  198. } else {
  199. return validate();
  200. }
  201. }
  202. });
  203. })(jQuery);