spngvld.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. 
  2. var spngvld = window.NameSpace || {};
  3. // 表单验证,利用class名来获取对象
  4. spngvld.addlblError = function (obj) {
  5. var lblError = "<label class='sp-error'></label>";
  6. if ($(obj).parent().children(".sp-error").length < 1) {
  7. $(obj).after(lblError);
  8. $(obj).parent().addClass('sp-text-nowrap').addClass("sp-relative");
  9. }
  10. }
  11. // 整合验证
  12. spngvld.getblur = function (obj, regex, tip) {
  13. var className = $(obj).attr('class');
  14. var str = $.trim($(obj).val());
  15. var strReg = !!str.match(regex);
  16. if (str != "" && strReg == false) {
  17. $(obj).next("label").show().text(tip);
  18. $(obj).removeClass("sp-rightLogo").addClass('sp-errorTip').addClass('sp-errorLogo');
  19. $(obj).focus();
  20. return false;
  21. }else if (str != "" && strReg == true) {
  22. $(obj).removeClass('sp-errorTip').removeClass('sp-errorLogo').addClass("sp-rightLogo");
  23. $(obj).next("label").hide().text("");
  24. }
  25. if (className.indexOf('sp-required') == -1) {
  26. if (str == "") {
  27. $(obj).removeClass('sp-errorTip').removeClass('sp-errorLogo');
  28. $(obj).next("label").hide().text("");
  29. }
  30. }
  31. }
  32. //1.验证必填 sp-required
  33. app.directive("spRequired", function () {
  34. return {
  35. restrict: "A",
  36. link: function (scope, elem, attrs) {
  37. //用于控制在特定时机设为必填
  38. // if ($(elem).attr("sp-required") == "true") {
  39. spngvld.addlblError(elem);
  40. $(elem).addClass("sp-required").addClass("sp-requireLogo");
  41. $(elem).blur(function () {
  42. var newVal = $.trim($(elem).val());
  43. var getCalss = $(elem).attr("class");
  44. if (newVal == "") {
  45. $(elem).next("label").show().html("必填");
  46. $(elem).addClass('sp-errorTip').removeClass('sp-requireLogo').removeClass('sp-rightLogo').addClass('sp-errorLogo');
  47. if ($(elem).attr("readonly") == "readonly") {
  48. setTimeout(function () {
  49. $(elem).next("label").hide().html("");
  50. $(elem).removeClass('sp-errorTip').removeClass('sp-errorLogo')
  51. },1000)
  52. }
  53. }
  54. else {
  55. if (getCalss.indexOf("sp-url") == -1 && getCalss.indexOf("sp-tel") == -1 && getCalss.indexOf("sp-telephone") == -1 && getCalss.indexOf("sp-email") == -1 && getCalss.indexOf("sp-username") == -1) {
  56. $(elem).next("label").hide().html("");
  57. $(elem).removeClass('sp-errorTip').removeClass('sp-errorLogo');
  58. }
  59. }
  60. })
  61. // }
  62. }
  63. }
  64. });
  65. //2.验证手机号 sp-phone
  66. app.directive("spPhone", function () {
  67. return {
  68. restrict: "A",
  69. link: function (scope, elem, attrs) {
  70. $(elem).addClass("sp-phone");
  71. spngvld.addlblError(elem);
  72. $(elem).blur(function () {
  73. ///var regex = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;
  74. var regex = /^1[3456789]\d{9}$/;
  75. spngvld.getblur(elem, regex, "手机号错误");
  76. });
  77. }
  78. }
  79. });
  80. //3.验证座机号码 sp-tel
  81. app.directive("spTel", function () {
  82. return {
  83. restrict: "A",
  84. link: function (scope, elem, attrs) {
  85. $(elem).addClass("sp-tel");
  86. spngvld.addlblError(elem);
  87. $(elem).blur(function () {
  88. var regex = /^(?:(?:0\d{2,3})-)?(?:\d{7,8})(-(?:\d{3,}))?$/;
  89. spngvld.getblur(elem, regex, "座机号错误");
  90. });
  91. }
  92. }
  93. });
  94. //4.验证联系电话 sp-telephone
  95. app.directive("spTelephone", function () {
  96. return {
  97. restrict: "A",
  98. link: function (scope, elem, attrs) {
  99. $(elem).addClass("sp-telephone");
  100. spngvld.addlblError(elem);
  101. $(elem).blur(function () {
  102. var tel = $.trim($(this).val());
  103. if (tel.substring(0, 1) == 1) {
  104. var regex = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;
  105. spngvld.getblur(this, regex, "手机号错误");
  106. }
  107. else {
  108. var regex = /^(?:(?:0\d{2,3})-)?(?:\d{7,8})(-(?:\d{3,}))?$/;
  109. spngvld.getblur(this, regex, "座机号错误");
  110. }
  111. });
  112. }
  113. }
  114. });
  115. //5.验证邮箱 sp-email
  116. app.directive("spEmail", function () {
  117. return {
  118. restrict: "A",
  119. link: function (scope, elem, attrs) {
  120. $(elem).addClass("sp-email");
  121. spngvld.addlblError(elem);
  122. $(elem).blur(function () {
  123. var regex = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
  124. spngvld.getblur(elem, regex, "邮箱错误");
  125. });
  126. }
  127. }
  128. });
  129. //6.验证身份证号 sp-card
  130. app.directive("spCard", function () {
  131. return {
  132. restrict: "A",
  133. link: function (scope, elem, attrs) {
  134. spngvld.addlblError(elem);
  135. $(elem).blur(function () {
  136. var regex = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
  137. spngvld.getblur(elem, regex, "身份证错误");
  138. });
  139. }
  140. }
  141. });
  142. //7.验证邮编 sp-postcode
  143. app.directive("spPostcode", function () {
  144. return {
  145. restrict: "A",
  146. link: function (scope, elem, attrs) {
  147. spngvld.addlblError(elem);
  148. $(elem).blur(function () {
  149. var regex = /^[0-9][0-9]{5}$/;
  150. spngvld.getblur(elem, regex, "邮编错误");
  151. });
  152. }
  153. }
  154. });
  155. //8. 验证传真 sp-fax
  156. app.directive("spFax", function () {
  157. return {
  158. restrict: "A",
  159. link: function (scope, elem, attrs) {
  160. spngvld.addlblError(elem);
  161. $(elem).blur(function () {
  162. var regex = /^(?:(?:0\d{2,3})-)?(?:\d{7,8})(-(?:\d{3,}))?$/;
  163. spngvld.getblur(elem, regex, "传真号错误");
  164. });
  165. }
  166. }
  167. });
  168. //9.验证网址 sp-url
  169. app.directive("spUrl", function () {
  170. return {
  171. restrict: "A",
  172. link: function (scope, elem, attrs) {
  173. $(elem).addClass("sp-url");
  174. spngvld.addlblError(elem);
  175. elem.blur(function () {
  176. var regex = /((https|http|ftp|rtsp|mms):\/\/)?(([0-9a-z_!~*'().&=+$%-]+:)?[0-9a-z_!~*'().&=+$%-]+@)?(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-z_!~*'()-]+\.)*([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\.[a-z]{2,6})(:[0-9]{1,4})?((\/?)|(\/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+\/?)/g;
  177. spngvld.getblur(elem, regex, "网址错误");
  178. });
  179. }
  180. }
  181. });
  182. //10.验证IP地址 sp-ip
  183. app.directive("spIp", function () {
  184. return {
  185. restrict: "A",
  186. link: function (scope, elem, attrs) {
  187. spngvld.addlblError(elem);
  188. $(elem).blur(function () {
  189. var regex = /^\d+\.\d+\.\d+\.\d+$/;
  190. spngvld.getblur(elem, regex, "IP错误");
  191. });
  192. }
  193. }
  194. });
  195. //11.验证用户名合法 sp-username
  196. app.directive("spUsername", function () {
  197. return {
  198. restrict: "A",
  199. link: function (scope, elem, attrs) {
  200. $(elem).addClass("sp-username");
  201. spngvld.addlblError(elem);
  202. $(elem).blur(function () {
  203. var regex = /^(?![^a-zA-Z]+$)(?!\D+$).{5,20}$/;
  204. spngvld.getblur(elem, regex, "用户名应由5-20位字母、数字组成");
  205. });
  206. }
  207. }
  208. });
  209. //12.验证数字 sp-digit
  210. app.directive("spDigit", function () {
  211. return {
  212. restrict: "A",
  213. link: function (scope, elem, attrs) {
  214. spngvld.addlblError(elem);
  215. $(elem).blur(function () {
  216. var regex = /^[0-9]*$/;
  217. spngvld.getblur(elem, regex, "请输入数字");
  218. });
  219. }
  220. }
  221. });
  222. //13.验证小数 sp-decimal
  223. app.directive("spDecimal", function () {
  224. return {
  225. restrict: "A",
  226. link: function (scope, elem, attrs) {
  227. spngvld.addlblError(elem);
  228. $(elem).blur(function () {
  229. var regex = /^\d+(\.\d+)?$/;
  230. spngvld.getblur(elem, regex, "请输入数字或小数");
  231. });
  232. }
  233. }
  234. });
  235. //14.验证中文 sp-chinese
  236. app.directive("spChinese", function () {
  237. return {
  238. restrict: "A",
  239. link: function (scope, elem, attrs) {
  240. spngvld.addlblError(elem);
  241. $(elem).blur(function () {
  242. var regex = /^[\u4E00-\u9FFF]+$/;
  243. spngvld.getblur(elem, regex, "请输入中文");
  244. });
  245. }
  246. }
  247. });
  248. //15.验证密码 sp-password
  249. app.directive("spPassword", function () {
  250. return {
  251. restrict: "A",
  252. link: function (scope, elem, attrs) {
  253. var className = $(elem).attr('class');
  254. var spanpwd = "<span class='sp-strength-tip'></span>";
  255. spngvld.addlblError(elem);
  256. if ($(elem).parent().children(".sp-strength-tip").length < 1) {
  257. $(elem).next("label").after(spanpwd);
  258. }
  259. $(elem).blur(function () {
  260. $(".sp-strength-tip").hide();
  261. $(elem).removeClass("sp-rightLogo");
  262. var pwd = $.trim($(elem).val());
  263. var pwdReg = !!pwd.match(/^(?![^a-zA-Z]+$)(?!\D+$).{6,30}$/);//字母数字
  264. var pwdRegCheck = !!pwd.match(/^(?=.*[a-zA-Z])(?=.*\d)(?=.*[~!@#$%^&*()_+`\-={}:";'<>?,.\/]).{6,30}$/);//字母数字特殊符号
  265. if (pwd != "") {
  266. if (pwdReg == false) {
  267. $(elem).next("label").addClass("arrow-top").show().html("密码应由6~30位字母、数字组成");
  268. $(elem).removeClass("sp-rightLogo").addClass('sp-errorTip'); $(this).focus();
  269. return false;
  270. } else {
  271. $(elem).removeClass('sp-errorTip').removeClass('sp-errorLogo').addClass("sp-rightLogo");
  272. $(elem).next("label").hide().html("");
  273. if (pwd.length < 9) {
  274. $(elem).parent().find(".sp-strength-tip").show().text("强度低");
  275. } else {
  276. $(elem).parent().find(".sp-strength-tip").show().text("强度中");
  277. }
  278. }
  279. //若含有特殊字符则表示 强度高
  280. if (pwdRegCheck == true && pwd.length > 9) {
  281. $(elem).removeClass('sp-errorTip').removeClass('sp-errorLogo').addClass("sp-rightLogo");
  282. $(elem).next("label").hide().html("");
  283. $(elem).parent().find(".sp-strength-tip").show().text("强度高");
  284. }
  285. }
  286. if (className.indexOf('sp-required') == -1) {
  287. if (pwd == "") {
  288. $(elem).removeClass('sp-errorTip');
  289. $(elem).next("label").hide().html("");
  290. }
  291. }
  292. });
  293. }
  294. }
  295. });
  296. //提交时检测 所需要验证的 控件是否合法, 不合法就不能通过,n为2时前端只标红提醒不禁用后续操作
  297. spngvld.submit = function (formID, n) {
  298. /// <summary>提交时检测form表单格式的合法性</summary>
  299. var result = true;
  300. $(formID + " label.sp-error").each(function () {
  301. var getClass = $(this).prev().attr("class"); //获取当前节点的前一个节点
  302. if (getClass.indexOf("sp-required") != -1) { //如果含有sp-required必填
  303. //含有必填,其值为空时
  304. if ($(this).prev().val() == "" || $(this).prev().val() == null) {
  305. if ($(this).is(":hidden")) {
  306. $(this).show().html("必填");
  307. $(this).prev().addClass('sp-errorTip').removeClass('sp-requireLogo').addClass('sp-errorLogo');
  308. }
  309. if (n != 2) {
  310. result = false;
  311. }
  312. }
  313. if ($(this).prev().val() != "") {
  314. if ($(this).is(":visible")) {
  315. if (n != 2) {
  316. result = false;
  317. }
  318. }
  319. }
  320. }
  321. //如果不是必填,但含有其他验证 时
  322. if (getClass.indexOf("sp-required") == -1) {
  323. if ($(this).prev().val() != "") {
  324. if ($(this).is(":visible")) {
  325. if (n != 2) {
  326. result = false;
  327. }
  328. }
  329. }
  330. }
  331. });
  332. if (n != 2) {
  333. $(formID + " .sp-error:visible:eq(0)").prev().focus(); //将焦点定位到第一个sp-error所属的文本框中
  334. }
  335. return result;
  336. }
  337. //成功时清除掉“对号”图片logo
  338. spngvld.clear = function (formID) {
  339. $(formID + " .sp-rightLogo").each(function () {
  340. $(this).removeClass("sp-rightLogo");
  341. $(this).next("label").hide();
  342. });
  343. $(formID + " .sp-required").each(function () {
  344. $(this).removeClass("sp-errorTip").removeClass("sp-errorLogo").addClass("sp-requireLogo");
  345. $(this).next("label").hide();
  346. });
  347. }