首页 > 语言 > JavaScript > 正文

推荐一个自己用的封装好的javascript插件

2024-05-06 16:15:30
字体:大 中 小
来源:转载
供稿:网友

本文推荐给大家一款自用的封装好的javascript插件,常用的一些检测全部都涵盖了,非常实用,推荐给小伙伴们。

具体内容请看注释,这里就不多BB了,

奉上代码:

 

 
  1. /// <reference path="vendor/jquery-1.4.1-vsdoc.js" /> 
  2. ///检测表单中的不能为空(.notnull)的验证 
  3. /* 
  4. 时间:2012-6-6 
  5. 作用:一对form标签下有多个(包括一个)表单需要提交时,使用js准确的判断当前按钮对那些元素做判断 
  6. 用法:在form标签下 找到当前 表单的容器 给予class="form",当前表单的提交按钮给予 class="check" 
  7. 需要验证为空的元素给予class="notnull" nullmsg="xx不能为空!"提示,需要进行逻辑判断的表单给予class="need" 
  8. 判断的类型给予 class="num"(只能是数字) 验证的提示 logicmsg="XX只能是数字" 
  9. 给予class="errorMessage"显示错误信息块 
  10. 给予class="warn"显示错误信息 
  11. 未使用js面向对象编程 
  12. 逻辑判断,不传入need标识,直接给出正则表达式属性(自定义)regex="/^/d$/" 做出判断 
  13. 在外部实现 
  14. Global.submitCallback button回调函数 
  15. Global.confirmCallback confirm回调函数; 
  16. 需要改进的地方: 
  17. 暂无 
  18. 更新时间:2014年12月3日 16:23:22 
  19. 作者:Amber.Xu 
  20. */ 
  21. //$(document).ready( 
  22. // function () { 
  23. // $("form").find(".notnull").bind({ 
  24. // focus: function () { 
  25. // if ($(this).attr("value") == this.defaultValue) { 
  26. // $(this).attr("value", ""); 
  27. // } 
  28. // }, 
  29. // blur: function () { 
  30. // if ($(this).attr("value") == "") { 
  31. // $(this).attr("value", this.defaultValue); 
  32. // } 
  33. // } 
  34. // }); 
  35. // } 
  36. //); 
  37. ///*封装一个万能检测表单的方法*/ 
  38. ///event.srcElement:引发事件的目标对象,常用于onclick事件。 
  39. ///event.fromElement:引发事件的对象源,常用于onmouseout和onmouseover事件。 
  40. ///event.toElement:引发事件后,鼠标移动到的目标源,常用于onmouseout和onmouseover事件。 
  41. function Global() { 
  42. var _self = this; 
  43. } 
  44. Global.submitCallback = null; 
  45. Global.confirmCallback = null; 
  46. $(document).ready(function () { 
  47. //form body 
  48. $("body").find(".form").each(function () { 
  49. this.onclick = function (e) { 
  50. var button = null; 
  51. try { 
  52. button = e.srcElement == null ? document.activeElement : e.srcElement; 
  53. } catch (e) { 
  54. console.log(e.message) 
  55. button = document.activeElement; 
  56. } 
  57. if ($(button).is(".check")) { 
  58. //alert("提交") 
  59. var sub = (checkform(this) && CheckInputRex(this) && checkselect(this) && checkChecked(this)); 
  60. if (sub) { 
  61. // Call our callback, but using our own instance as the context 
  62. Global.submitCallback.call(this, [e]); 
  63. } 
  64. return sub; 
  65. } else if ($(button).is(".confirm")) { 
  66. //alert("删除") 
  67. var sub = confirm($(button).attr("title")); 
  68. if (sub) { 
  69. Global.confirmCallback.call(this, [e]); 
  70. } 
  71. return sub; 
  72. } else { 
  73. // //alert("其它") 
  74. return true; 
  75. } 
  76. } 
  77. }); 
  78. /*检测表单中不能为空的元素*/ 
  79. function checkform(form) { 
  80. var b = true; 
  81. $(form).find(".notnull").each(function () { 
  82. if ($.trim($(this).val()).length <= 0) {//|| $(this).val() == this.defaultValue 
  83. // if (this.value != null) { 
  84. // $(this).attr("value", ""); 
  85. // } 
  86. //alert($(this).attr("msg")) 
  87. $(this).parents(".form").find(".warn").text($(this).attr("nullmsg")); 
  88. $(this).parents(".form").find(".errorMessage").show(); 
  89. $(this).select(); 
  90. $(this).focus(); 
  91. return b = false; 
  92. } 
  93. }); 
  94. if (b == true) { 
  95. $(form).find(".warn").text(""); 
  96. $(form).find(".errorMessage").hide(); 
  97. } 
  98. return b; 
  99. } 
  100. /*检测表单中必选的下拉列表*/ 
  101. function checkselect(form) { 
  102. var b = true; 
  103. $(form).find(".select").each(function (i) { 
  104. var ck = $(this).find('option:selected').text(); 
  105. if (ck.indexOf("选择") > -1) { 
  106. $(this).parents(".form").find(".warn").text($(this).attr("nullmsg")); 
  107. $(this).parents(".form").find(".errorMessage").show(); 
  108. $(this).select(); 
  109. $(this).focus(); 
  110. return b = false; 
  111. } 
  112. }); 
  113. return b; 
  114. } 
  115. /*检测表单中必选的复选框*/ 
  116. function checkChecked(form) { 
  117. var b = true; 
  118. $(form).find(".checkbox").each(function (i) { 
  119. var ck = $(this)[0].checked; 
  120. if (!ck) { 
  121. $(this).parents(".form").find(".warn").text($(this).attr("nullmsg")); 
  122. $(this).parents(".form").find(".errorMessage").show(); 
  123. $(this).select(); 
  124. $(this).focus(); 
  125. return b = false; 
  126. } 
  127. }); 
  128. return b; 
  129. } 
  130. //检查是否匹配该正则表达式 
  131. function GetFlase(value, reg, ele) { 
  132. if (reg.test(value)) { 
  133. return true; 
  134. } 
  135. $(ele).parents(".form").find(".warn").text($(ele).attr("logicmsg")); 
  136. $(ele).parents(".form").find(".errorMessage").show(); 
  137. $(ele).focus(); 
  138. $(ele).select(); 
  139. return false; //不能提交 
  140. } 
  141. function CheckInputRex(form) { 
  142. var b = true; 
  143. $(form).find("input[type='text']").each(function () { 
  144. if (typeof ($(this).attr("regex")) == 'string') { 
  145. if ($.trim($(this).val()).length > 0 && $(this).val() != this.defaultValue) { 
  146. //当前表单的值 
  147. var value = $(this).attr("value") || $(this).val(); 
  148. var regx = eval($(this).attr("regex")); 
  149. return b = GetFlase(value, regx, this); 
  150. } 
  151. } 
  152. }); 
  153. return b; 
  154. } 
  155. ///检查用户输入的相应的字符是否合法 
  156. ///此方法已废弃 
  157. function CheckInput(form) { 
  158. var b = true; 
  159. $(form).find(".need").each(function () { 
  160. if ($.trim($(this).val()).length > 0 && $(this).val() != this.defaultValue) { 
  161. //当前表单的值 
  162. var value = $(this).attr("value"); 
  163. //id的值或者name的属性的值如:[name="contact"] 
  164. var name = $(this).attr("class"); 
  165. //检查需要输入的内容是否合法如:联系方式 
  166. var len = name.split(" "); 
  167. for (var i = 0; i < len.length; i++) { 
  168. switch ($.trim(len[i])) { 
  169. ///联系方式  
  170. case "mobile": 
  171. var reg = /^1/d{10}$/; 
  172. return b = GetFlase(value, reg, this); 
  173. break; 
  174. ///邮箱  
  175. case "email": 
  176. var reg = /^[/w-]+(/.[/w-]+)*@[/w-]+(/.[/w-]+)+$/; 
  177. return b = GetFlase(value, reg, this); 
  178. break; 
  179. ///两次密码是否一致  
  180. case "password": 
  181. break; 
  182. case "password2": 
  183. if ($("#password").attr("value") != $("#password2").attr("value")) { 
  184. $(this).select(); //获取焦点 
  185. $(this).parents(".form").find(".warn").text($(this).attr("logicmsg")); 
  186. $(this).parents(".form").find(".errorMessage").show(); 
  187. return b = false; //不能提交 
  188. } 
  189. break; 
  190. case "worktel": 
  191. case "hometel": //家庭电话 
  192. var reg = /^/d{8}$/; 
  193. return b = GetFlase(value, reg, this); 
  194. break; 
  195. case "post": //邮编 
  196. var reg = /^/d{6}$/; 
  197. return b = GetFlase(value, reg, this); 
  198. break; 
  199. case "bonus": 
  200. case "allowance": 
  201. case "FixedSalary": 
  202. var reg = /^-?([1-9]/d*/./d*|0/./d*[1-9]/d*|0?/.0+|0|[1-9]/d)$/; 
  203. return b = GetFlase(value, reg, this); 
  204. break; 
  205. case "identity": 
  206. var reg = /(^/d{15}$)|(^/d{18}$)|(^/d{17}(/d|X|x)$)/; 
  207. return b = GetFlase(value, reg, this); 
  208. break; 
  209. case "height": 
  210. var reg = /^[1-2][0-9][0-9]$/; 
  211. return b = GetFlase(value, reg, this); 
  212. break; 
  213. case "qq": 
  214. var reg = /^[1-9][0-9]{4,}$/; 
  215. return b = GetFlase(value, reg, this); 
  216. break; 
  217. case "begintime": 
  218. case "endtime": 
  219. var reg = /^/d{4}$/; 
  220. if (reg.test(value) && (parseInt($(".endtime").val()) > parseInt($(".begintime").val()))) { 
  221. return b; 
  222. } 
  223. $.ligerDialog.alert($(this).attr("msg")) 
  224. $(this).select(); //获取焦点 
  225. return b = false; //不能提交 
  226. break; 
  227. case "num": 
  228. var reg = /^/d+$/; 
  229. return b = GetFlase(value, reg, this); 
  230. break; 
  231. ///大陆去香港需要办理往来港澳通行证和香港的签注.因私普通护照号码格式有:  
  232. ///14/15+7位数,G+8位数;  
  233. ///因公普通的是:P.+7位数;  
  234. ///公务的是:S.+7位数 或者  
  235. //S+8位数,以D开头的是外交护照  
  236. case "postport": //护照号码 
  237. var reg = /^(P/d{7}|G/d{8}|S/d{7,8}|D/d+|1[4,5]/d{7})$/; 
  238. return b = GetFlase(value, reg, this); 
  239. break; 
  240. case "bankaccount": 
  241. var reg = /^[0-9]{19}$/; 
  242. return b = GetFlase(value, reg, this); 
  243. break; 
  244. } //switch 
  245. } //for 
  246. } 
  247. }); 
  248. return b; 
  249. } 
  250. ///此方法已经废弃 
  251. }); 
  252. ///单击改变背景颜色 
  253. $(document).ready(function () { 
  254. var inputs = $("#top>.c>input"); 
  255. $(inputs).each(function () { 
  256. this.onclick = function () { 
  257. document.getElementById("main").style.backgroundColor = this.name; 
  258. //$("#main").backgroundColor = this.name; 
  259. } 
  260. }); 
  261. }); 

基本上常用的功能都封装在内了,希望小伙伴们能够喜欢。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选