validation.js 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562
  1. /**
  2. * 用于处理表单验证的JS文件 110609 注意:iframe默认是有最大的宽高限制的,要想iframe加载的页面较大时
  3. * 也能自动调整大小,应该为iframe的css样式增加:width: 100%;height: 100%; 否则iframe大到一定程序就不能再大了
  4. */
  5. //var validationManagerPath = '/wd/js/validation';
  6. function isMobile() {
  7. const screenWidth = (window !== window.top ? window.top : window).innerWidth <= 768;
  8. const userAgent = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
  9. // 检测触摸支持
  10. const hasTouchScreen = (
  11. 'ontouchstart' in window ||
  12. navigator.maxTouchPoints > 0 ||
  13. navigator.msMaxTouchPoints > 0
  14. );
  15. return hasTouchScreen && (userAgent || screenWidth);
  16. }
  17. // 为真,则在用户输入数据时实现进行检查
  18. var realTimeCheck = true;
  19. var chartDiv;
  20. var validationTimeOutId = null;
  21. if (realTimeCheck) {
  22. // 如果需要在用户输入数据时实现进行检查,则把相应的iframe写入页面
  23. /*
  24. * var divStr = '<div style="position: absolute; z-index: 19701; top:
  25. * -500px" id="msgDiv"><div id="validationDiv"><iframe name="msgIframe"
  26. * id="msgIframe" src="'+validationManagerPath+ '/validationMsg.html"
  27. * scrolling="no" frameborder="0" style="visibility: visible;position:
  28. * absolute; width: 100%; height: 100%;"></iframe></div></div>'; var
  29. * topBody = top.document.getElementsByTagName('BODY')[0]; topBody.innerHTML =
  30. * divStr;
  31. */
  32. // alert(window.top.document.getElementsByTagName("body")[0]);
  33. // document.write('<div style="position: absolute; z-index: 65535; top:
  34. // -500px" id="msgDiv"><div id="validationDiv"><iframe name="msgIframe"
  35. // id="msgIframe" src="'+validationManagerPath+
  36. // '/validationMsg.html" scrolling="no" frameborder="0"
  37. // style="visibility: visible;position: absolute; width: 100%; height:
  38. // 100%;"></iframe></div></div>');
  39. var topDoc = top.document;
  40. chartDiv = topDoc.getElementById("msgDiv");
  41. if (chartDiv == null || typeof chartDiv == "undefined") {
  42. chartDiv = topDoc.createElement("div");
  43. topDoc.body.appendChild(chartDiv);
  44. chartDiv.id = "msgDiv";
  45. chartDiv.innerHTML +=
  46. '<div id="validationDiv" class="flashMsg-div" ><span class="msgDiv-content"></span><div class="msgDiv-tail"></div></div>';
  47. // chartDiv.innerHTML = '<img
  48. // src="<%=request.getContextPath()%>/images/kpiUp.jpg" />';//错误的
  49. with (chartDiv.style) {
  50. zIndex = 65535;
  51. position = "absolute";
  52. // background = '#F5F5F5';
  53. width = "auto";
  54. height = 0;
  55. top = -500;
  56. left = 0;
  57. // border = "1px solid";
  58. display = "block";
  59. style = "position:absolute;border-radius:3px;overflow:hidden;";
  60. }
  61. }
  62. }
  63. // 保存校验结果的变量
  64. var message = "";
  65. function ValidationManager() {
  66. // 用于处理表单验证的工具类构造器
  67. /** 保存所有要校验的信息的集合* */
  68. this.validationInfos = [];
  69. }
  70. ValidationManager.MSG_TYPE_SUCCESS = 0; // 消息类型――正确
  71. ValidationManager.MSG_TYPE_ERROR = 1; // 消息类型――错误
  72. ValidationManager.MSG_TYPE_WARNING = 2; // 消息类型――警告
  73. ValidationManager.prototype = {
  74. /** 用于显示表单验证结果的Div* */
  75. MsgDiv: chartDiv,
  76. /** 如果为真,则如果代码报错会alert出来,在实际项目中应设为false* */
  77. debug: true,
  78. /** 通过NAME或ID获得页面元素的通用方法* */
  79. getElement: function (nameOrId) {
  80. var rs = document.getElementById(nameOrId);
  81. if (!rs) {
  82. // 如果上面的方法失败,则采用下面的方法取得对象实例
  83. var arr = document.getElementsByName(nameOrId);
  84. if (arr != null) rs = arr[0];
  85. }
  86. console.log(rs);
  87. return rs;
  88. },
  89. getElements: function (nameOrId) {
  90. var rs = new Array();
  91. var ele = document.getElementById(nameOrId);
  92. if (!ele) {
  93. // 如果上面的方法失败,则采用下面的方法取得对象实例
  94. var arr = document.getElementsByName(nameOrId);
  95. if (arr != null) {
  96. for (var i = 0; i < arr.length; i++) {
  97. rs.push(arr[i]);
  98. }
  99. }
  100. } else {
  101. rs.push(ele);
  102. }
  103. // console.log(rs)
  104. return rs;
  105. },
  106. /** 获取参数s指定的方法引用* */
  107. getFunRef: function (s) {
  108. if (!s) return null;
  109. try {
  110. var r = eval(s);
  111. } catch (e) {
  112. //alert("方法不存在 :"+s)
  113. console.log("方法不存在 :" + s);
  114. }
  115. return r;
  116. },
  117. /**
  118. * 为DOM元素targetElem的evenName事件追加事件触发时回调函数activeFun
  119. * 通过此函数为DOM元素绑定监听事件的好处在于:可以把新添加的监听函数与DOM元素
  120. * 已绑定的旧的监听函数合并成为新的监听函数,而不会覆盖DOM元素旧的同名监听函数。
  121. */
  122. addEventListener: function (
  123. targetElem, // 需绑定监听事件的DOM元素
  124. eventName, // 事件名称,如:onclick
  125. activeFun
  126. ) {
  127. // 事件触发的回调函数
  128. // DOM元素原来的监听事件
  129. var oriListener = targetElem[eventName];
  130. if (oriListener) {
  131. // 如果原来已经存在同名的监听事件了
  132. targetElem[eventName] = function () {
  133. oriListener.call(targetElem); // 调用原来已经存在监听方法
  134. activeFun.call(targetElem); // 调用新添加的监听方法
  135. };
  136. } else {
  137. targetElem[eventName] = function () {
  138. activeFun.call(targetElem); // 调用新添加的监听方法
  139. };
  140. }
  141. },
  142. /** 当前页面的第一个FORM元素* */
  143. form: null,
  144. /** 用于显示表单验证结果的iframe的Id或Name* */
  145. MsgIframeIdorName: "msgIframe",
  146. /** 用于显示表单验证结果的div的Id或Name* */
  147. MsgDivIdorName: "msgDiv",
  148. /** 用于显示表单验证结果的iframe* */
  149. MsgIframe: null,
  150. /** 已经初始化成功* */
  151. initOk: false,
  152. /**
  153. * 如果表单存在,则为表单绑定onsubmit校验事件
  154. *
  155. * @param {}
  156. * targetForm 需要校验的FORM的name或者ID
  157. * @return {Boolean} 返回校验结果,为真则校验通过
  158. */
  159. checkForm: function (targetForm) {
  160. try {
  161. var THIS = this;
  162. if (!targetForm) {
  163. // 如果目标form不存在
  164. //alert('checkForm方法执行出错,参数不能为空!');
  165. console.log("checkForm方法执行出错,参数不能为空!");
  166. return;
  167. }
  168. if ((typeof targetForm).toUpperCase() == "STRING")
  169. // 如果输入的是FORM的name或者ID
  170. var targetForm = this.getElement(targetForm);
  171. if (!targetForm.validationManager) {
  172. // 如果FORM中没校验器,则添加一个校验器
  173. targetForm.validationManager = this;
  174. var activeFun = function (showerror) {
  175. // 为表单绑定onsubmit事件
  176. try {
  177. if (window.beforeBatchSubmit) {
  178. window.beforeBatchSubmit();
  179. }
  180. } catch (e) {
  181. console.log(e);
  182. }
  183. var rsArr = targetForm.validationManager.checkAll(
  184. targetForm.validationManager.validationInfos
  185. );
  186. console.log(rsArr)
  187. var totalMsg = ""; // 保存校验显示的消息
  188. for (var i = 0; i < rsArr.length; i++) {
  189. var rs = rsArr[i];
  190. var msg = rs.msg;
  191. var regExp = /[\n\r]+$/; // 测试校验结果结尾有没有换行符的正则表达式
  192. var hasLineBreak; // 有换行符
  193. hasLineBreak = regExp.test(msg);
  194. // if(hasLineBreak)//如果有换行符
  195. // totalMsg+=msg;
  196. // else
  197. totalMsg += msg + "<br>";
  198. }
  199. if (totalMsg) {
  200. // 如果校验不通过
  201. if (showerror == false) return false; //如果不显示错误
  202. try {
  203. var windowId = new Date().getTime();
  204. var width = 400;
  205. var high = 400;
  206. // wd.topWindow.dhxWins.enableAutoViewport(true);//让窗口大小自动适应内容
  207. var w = wd.topWindow.dhxWins.createWindow(
  208. windowId,
  209. 0,
  210. 0,
  211. width,
  212. high
  213. );
  214. //----------------加遮罩层开始 ----------------
  215. if (!wd.topWindow.wd.display.wdDialogOpeners) {
  216. wd.topWindow.wd.display.wdDialogOpeners = {};
  217. }
  218. var wdDialogId = windowId;
  219. wd.topWindow.wd.display.wdDialogOpeners[wdDialogId] = window;
  220. var containerObj = wd.topWindow.wd.display;
  221. if (wd.topWindow.document.querySelector("#validationTotalMsg")) {
  222. wd.topWindow.document
  223. .querySelector("#validationTotalMsg")
  224. .close();
  225. }
  226. //----------------加遮罩层结束 ----------------
  227. w.keepInViewport(true); // 保持在窗口内
  228. w.center(); // 居中
  229. var title =
  230. "<span style=\"font-size: 18px; '宋体';line-height:30px; color: #333;margin-left: 5px;\" >录入数据错误</span>";
  231. w.setText(title); // 设置窗口标题
  232. w.setModal(true); // 设置模式窗口
  233. w.bringToTop(); // 置顶
  234. // 隐藏不需要的按钮
  235. // alert(document.getElementById("mainContainer")==wd.topWindow.document.body);
  236. if (w.button("help")) w.button("help").hide();
  237. if (w.button("stick")) w.button("stick").hide();
  238. if (w.button("sticked")) w.button("sticked").hide();
  239. if (w.button("park")) w.button("park").hide();
  240. if (w.button("minmax1"))
  241. // 隐藏最大化按钮
  242. w.button("minmax1").hide();
  243. w.denyResize(); // 不允许改变大小
  244. var innerDiv =
  245. '<div id="innerFrame" class="scrollbar" style="width: ' +
  246. (width - 5) +
  247. ';height:calc(100% - 53px);display:flex;align-items:center;justify-content: space-around;overflow:auto;line-height:30px;font-size:16px;color:red;box-sizing:border-box;padding-left:15px;" >' +
  248. totalMsg +
  249. '</div><div class="bottom-div" style="position:absolute;"><div class="bottom-down-div"><input type="button" value="关闭" class="bottom-button" id="closeButton" style="letter-spacing: 6px;"></div></div></div>';
  250. w.attachHTMLString(innerDiv);
  251. w.setAttribute("id", "validationTotalMsg");
  252. var currentDialogZIndex = w.zi; // 弹出窗口的z-index
  253. wd.display.dxwindowsCreateMaskDiv(currentDialogZIndex);
  254. w.attachEvent("onClose", function (win) {
  255. wd.display.dxwindowsCloseMaskDiv(); //关闭wddialog时,移除遮罩层
  256. return true;
  257. });
  258. try {
  259. wd.display.initStyleWdButtons();
  260. $(w).on("click", "#closeButton", function () {
  261. w.close();
  262. });
  263. } catch (e) {
  264. console.log(e);
  265. }
  266. var divArr =
  267. wd.topWindow.document.querySelectorAll("[ida=dhxMainCont]");
  268. for (var i = 0; i < divArr.length; i++) {
  269. divArr[i].style.backgroundColor = "";
  270. }
  271. } catch (e) {
  272. //alert(totalMsg);// 弹出校验结果
  273. }
  274. THIS.hideMsgDiv();
  275. return false;
  276. } else return true;
  277. }; //end function
  278. //如果用户直接调用form的submit方法,则校验不会生效。因此,这里覆盖原来form的submit方法\
  279. targetForm.oriSubmit = targetForm.submit;
  280. targetForm.checkOnly = function (boo) {
  281. boo = typeof boo == "undefined" ? true : boo;
  282. // !(boo == null ? true : boo);
  283. return activeFun(boo);
  284. };
  285. targetForm.submit = function () {
  286. if (activeFun()) targetForm.oriSubmit();
  287. };
  288. wd.c.addEventListener(
  289. targetForm, //需绑定监听事件的DOM元素
  290. "onsubmit", //事件名称,如:onclick
  291. activeFun
  292. );
  293. }
  294. } catch (e) {
  295. if (this.debug)
  296. //alert('方法checkForm执行异常:'+e);
  297. console.log("方法checkForm执行异常:" + e);
  298. }
  299. },
  300. /** 如果为真则自动为当前页面的第一个表单绑定onsubmit校验事件* */
  301. formAutoCheck: true,
  302. /** 初始化变量的方法,只需执行一次* */
  303. init: function () {
  304. try {
  305. if (this.initOk) return; // 如果已经初始化过了,就返回。
  306. if (!this.MsgIframe)
  307. this.MsgIframe = this.getElement(this.MsgIframeIdorName);
  308. if (!this.MsgDiv) this.MsgDiv = this.getElement(this.MsgDivIdorName);
  309. if (this.formAutoCheck) {
  310. // var forms = document.getElementsByTagName('form');
  311. var forms = $("form:visible"); //过滤隐藏form
  312. this.form = forms[forms.length - 1]; // 当前页面的第一个FORM元素
  313. if (this.form) {
  314. // 如果表单存在,则为表单绑定onsubmit事件
  315. this.checkForm(this.form);
  316. }
  317. }
  318. this.initOk = true;
  319. } catch (e) {
  320. if (this.debug)
  321. //alert('方法init执行异常:'+e);
  322. console.log("方法init执行异常:" + e);
  323. }
  324. },
  325. /** 设定显示表单验证结果的iframe的显示内容* */
  326. setMsg: function (rsArr) {
  327. // try{
  328. // 调用iframe中的方法,设置表单验证结果的iframe的显示内容
  329. if (!rsArr || rsArr.length == 0) return;
  330. // console.log(rsArr)
  331. //this.MsgDiv.firstChild.innerHTML = rsArr[0].msg + "<div class=\"flashMsg-up warningFlashMsg-up\"></div>";
  332. this.MsgDiv.querySelector(".msgDiv-content").innerHTML = rsArr[0].msg;
  333. // 暂时隐藏20181128
  334. // window.top.frames[this.MsgIframeIdorName].setMsg(rsArr);
  335. // alert(document.frames.length);
  336. // }catch(e){
  337. // if(this.debug)
  338. // alert('方法setMsg执行异常:'+e.message);
  339. // }
  340. },
  341. /**
  342. * 返回传入对象e的绝对坐标,并作为一个对象返回
  343. *
  344. * @param {}
  345. * e
  346. * @return {"x": x, "y": y}
  347. */
  348. getAbsPoint: function (e) {
  349. try {
  350. var x = e.offsetLeft;
  351. var y = e.offsetTop;
  352. var w = e.offsetWidth;
  353. var h = e.offsetHeight;
  354. /**
  355. * OffsetParent为从中计算偏移量的元素。 如果某个元素的父级或此元素层次结构中的其他元素使用相对或绝对定位, 则 OffsetParent
  356. * 将成为当前元素嵌套到的第一个相对或绝对定位元素。如果当前元素以上的任何元素都不是绝对 或相对定位的,则 OffsetParent 将是文档的
  357. * BODY 标记。
  358. *
  359. * obj.offsetTop 指 obj 距离上方或上层控件的位置,整型,单位像素。 obj.offsetLeft 指 obj
  360. * 距离左方或上层控件的位置,整型,单位像素。
  361. *
  362. * offsetTop与offsetLeft均为相对偏移量,但是下面不断地拿offsetParent的偏移量来累加,
  363. * 直至拿到offsetParent为body之后,就没有offsetParent了,此时循环才终止。
  364. * 最终计算出来的x,y为e相对body的绝对坐标。
  365. */
  366. while ((e = e.offsetParent)) {
  367. // 只要对象的offsetParent存在,则继续累加XY,
  368. x += e.offsetLeft;
  369. y += e.offsetTop;
  370. }
  371. return {
  372. x: x,
  373. y: y,
  374. w: w,
  375. h: h,
  376. };
  377. } catch (e) {
  378. if (this.debug)
  379. //alert('方法getAbsPoint执行异常:'+e);
  380. console.log("方法getAbsPoint执行异常:" + e);
  381. }
  382. },
  383. /**
  384. * refleshIframeFun:function(){ if(!this.hasRefleshedIframe){
  385. * document.getElementById("validationDiv").innerHTML='<iframe name="msgIframe"
  386. * id="msgIframe" src="'+validationManagerPath+ '/validationMsg.html"
  387. * scrolling="no" frameborder="0" style="visibility: visible;position: absolute;
  388. * width: 100%; height: 100%;"></iframe>'; this.hasRefleshedIframe = true; } },
  389. */
  390. /**
  391. * MsgDiv定位于要校验的目标input元素的上方
  392. *
  393. * @param {}
  394. * targetInput 要校验的目标input元素
  395. */
  396. showMsgDiv: function (targetInput, msg) {
  397. try {
  398. wd.flashMsg.show({
  399. el: targetInput, // 吸附的元素
  400. msg: msg, // 内容
  401. animate: false,
  402. });
  403. } catch (e) {
  404. console.log(e);
  405. }
  406. if (true) return;
  407. // try{
  408. // this.refleshIframeFun();
  409. var win = window;
  410. var xy = this.recursionIframe(win);
  411. // var inputElem = document.getElementsByName(targetInput.name)[0];
  412. var offset = this.getInputOffset(targetInput, true);
  413. var height = this.MsgDiv.offsetHeight;
  414. this.MsgDiv.style.left = xy.x + offset.left + "px";
  415. var tailDiv = this.MsgDiv.querySelector(".msgDiv-tail");
  416. var msgtop;
  417. if (offset.top < window.innerHeight / 2) {
  418. //录入框下方,尾巴向上
  419. msgtop = xy.y + offset.top + 38 + "px";
  420. tailDiv.setAttribute("class", "msgDiv-tail flashMsg-up");
  421. } else {
  422. //录入框上方,尾巴向下
  423. msgtop = xy.y + offset.top - height - 38 + "px";
  424. tailDiv.setAttribute("class", "msgDiv-tail flashMsg-down");
  425. }
  426. this.MsgDiv.style.top = msgtop;
  427. // 调用隐藏的方法,让tips在3s后隐藏
  428. var dom = this.MsgDiv;
  429. function hideDom(dom) {
  430. dom.style.top = "-500px";
  431. }
  432. if (validationTimeOutId) clearTimeout(validationTimeOutId);
  433. validationTimeOutId = setTimeout(function () {
  434. hideDom(dom);
  435. }, 3000);
  436. /**
  437. * }catch(e){ if(this.debug) alert('方法执行异常:'+e); }
  438. */
  439. },
  440. /**
  441. * 获取输入光标在页面中的坐标 【..】
  442. *
  443. * @param {HTMLElement}
  444. * 输入框元素
  445. * @param {Boolean}
  446. * 返回焦点光标顶部的top值(默认false)
  447. * @return {Object} 返回left和top
  448. */
  449. getInputOffset: function (elem, focusTop) {
  450. var that = this,
  451. cloneDiv = "{$clone_div}",
  452. cloneLeft = "{$cloneLeft}",
  453. cloneFocus = "{$cloneFocus}",
  454. cloneRight = "{$cloneRight}",
  455. none = '<span style="white-space:pre-wrap;"> </span>',
  456. div = elem[cloneDiv] || document.createElement("div"),
  457. left = elem[cloneLeft] || document.createElement("span"),
  458. focus = elem[cloneFocus] || document.createElement("span"),
  459. right = elem[cloneRight] || document.createElement("span"),
  460. offset = that._offset(elem),
  461. index = this._getFocus(elem),
  462. focusOffset = {
  463. left: 0,
  464. top: 0,
  465. };
  466. div.style.width = this._getStyle(elem, "width");
  467. div.style.height = this._getStyle(elem, "height");
  468. try {
  469. div.scrollLeft = elem.scrollLeft;
  470. div.scrollTop = elem.scrollTop;
  471. } catch (e) {} // IE8
  472. left.innerHTML = ((elem.value && elem.value.substring(0, index)) || "")
  473. .replace(/</g, "&lt;")
  474. .replace(/>/g, "&gt;")
  475. .replace(/\n/g, "<br/>")
  476. .replace(/\s/g, none);
  477. right.innerHTML = (
  478. (elem.value && elem.value.substring(index, elem.value.length)) ||
  479. ""
  480. )
  481. .replace(/</g, "&lt;")
  482. .replace(/>/g, "&gt;")
  483. .replace(/\n/g, "<br/>")
  484. .replace(/\s/g, none);
  485. focus.style.display = "inline-block";
  486. try {
  487. focusOffset = this._offset(focus);
  488. } catch (e) {}
  489. focus.style.display = "none";
  490. return {
  491. left: offset.left + focusOffset.left,
  492. top: offset.top + focusOffset.top + (focusTop ? focus.offsetHeight : 0),
  493. };
  494. },
  495. // 获取元素在页面中位置
  496. _offset: function (elem) {
  497. var box = elem.getBoundingClientRect();
  498. var doc = elem.ownerDocument;
  499. var body = doc.body;
  500. var docElem = doc.documentElement;
  501. var clientTop = docElem.clientTop || body.clientTop || 0;
  502. var clientLeft = docElem.clientLeft || body.clientLeft || 0;
  503. var top = box.top + (self.pageYOffset || docElem.scrollTop) - clientTop;
  504. var left = box.left + (self.pageXOffset || docElem.scrollLeft) - clientLeft;
  505. return {
  506. left: left,
  507. top: top,
  508. };
  509. },
  510. // 获取光标在文本框的位置
  511. _getFocus: function (elem) {
  512. var index = 0;
  513. if (elem.nodeName === "TEXTAREA") {
  514. if (document.selection) {
  515. // IE Support
  516. elem.focus();
  517. var Sel = document.selection.createRange();
  518. var Sel2 = Sel.duplicate();
  519. Sel2.moveToElementText(elem);
  520. var index = -1;
  521. while (Sel2.inRange(Sel)) {
  522. Sel2.moveStart("character");
  523. index++;
  524. }
  525. } else if (elem.selectionStart || elem.selectionStart == "0") {
  526. // Firefox
  527. // support
  528. index = elem.selectionStart;
  529. }
  530. return index;
  531. } else {
  532. // input
  533. if (document.selection) {
  534. // IE Support
  535. elem.focus();
  536. var Sel = document.selection.createRange();
  537. Sel.moveStart("character", -elem.value.length);
  538. index = Sel.text.length;
  539. } else if (elem.selectionStart || elem.selectionStart == "0") {
  540. // Firefox
  541. // support
  542. index = elem.selectionStart;
  543. }
  544. return index;
  545. }
  546. },
  547. // 获取最终样式
  548. _getStyle:
  549. "getComputedStyle" in window
  550. ? function (elem, name) {
  551. return getComputedStyle(elem, null)[name];
  552. }
  553. : function (elem, name) {
  554. return elem.currentStyle[name];
  555. },
  556. /** 隐藏MsgDiv的方法* */
  557. hideMsgDiv: function () {
  558. // try{
  559. // this.MsgDiv.style.top = "-500px";
  560. /**
  561. * }catch(e){ if(this.debug) alert('方法showMsgD\niv执行异常:'+e); }
  562. */
  563. try {
  564. wd.flashMsg.clear();
  565. } catch (e) {
  566. console.log(e);
  567. }
  568. },
  569. /**
  570. * 为指定的input元素绑定校验方法的公共方法
  571. */
  572. initInput: function (validationInfo) {
  573. try {
  574. this.init(); // 初始化变量的方法
  575. this.validationInfos.push(validationInfo);
  576. for (var i = 0; i < validationInfo.formElems.length; i++) {
  577. // 被校验的表单元素
  578. var targetInput = validationInfo.formElems[i];
  579. // 保存校验信息到表单元素中
  580. if (!targetInput.validationInfos) {
  581. targetInput.validationInfos = [];
  582. }
  583. targetInput.validationInfos.push(validationInfo);
  584. // console.log(targetInput.validationInfos)
  585. /** 保存原来的borderBottomColor* */
  586. if (targetInput.style.borderBottomColor)
  587. targetInput.oldBorderColor = targetInput.style.borderBottomColor;
  588. else targetInput.oldBorderColor = "default";
  589. targetInput.validationManager = this;
  590. // $(targetInput).addClass("validation-"+targetInput.tagName.toLowerCase());
  591. $(targetInput).addClass("border-" + targetInput.tagName.toLowerCase());
  592. if (realTimeCheck) {
  593. // 如果要进行实时检查
  594. var activeFun = function () {
  595. //if(this.value!==''&&this.value!==null){
  596. this.validationManager.checkForRealTime(this); // 检查目标input元素的值
  597. /*
  598. }else{
  599. this.style.borderBottomColor='';//清空则去掉红框
  600. this.validationManager.hideMsgDiv();// 隐藏MsgDiv
  601. }
  602. */
  603. // targetInput.style.borderBottomColor="blue"
  604. };
  605. /** 为要校验的input元素绑定获得焦点事件* */
  606. this.addEventListener(
  607. targetInput, // 需绑定监听事件的DOM元素
  608. "onfocus", // 事件名称,如:onclick
  609. activeFun
  610. ); // 事件触发的回调函数
  611. this.mouseoverEvent(targetInput, activeFun);
  612. /** 为要校验的input元素绑定失去焦点事件* */
  613. var onblurFun = function () {
  614. // 隐藏MsgDiv 暂时去掉20181128
  615. // this.validationManager.hideMsgDiv();
  616. // targetInput.style.borderBottomColor=""
  617. };
  618. this.addEventListener(
  619. targetInput, // 需绑定监听事件的DOM元素
  620. "onblur", // 事件名称,如:onclick
  621. onblurFun
  622. ); // 事件触发的回调函数
  623. //初始化ueditor的校验事件
  624. if (
  625. typeof EditorManager != "undefined" &&
  626. EditorManager &&
  627. (validationInfo.argsObj.isUeditor == true ||
  628. validationInfo.argsObj.isUeditor == "true")
  629. ) {
  630. var vmr = this;
  631. window.ueCallback = window.ueCallback || [];
  632. var name = validationInfo.argsObj.editorName;
  633. window.ueCallback[name] = window.ueCallback[name] || [];
  634. window.ueCallback[name].push(function () {
  635. EditorManager.getWDEditor(validationInfo.argsObj.editorName)
  636. .getUeditor()
  637. .addListener("selectionchange", function () {
  638. // vmr.addEventListener(targetInput, // 需绑定监听事件的DOM元素
  639. // 'onblur', // 事件名称,如:onclick
  640. // onblurFun);
  641. // vmr.addEventListener(targetInput, // 需绑定监听事件的DOM元素
  642. // 'onfocus', // 事件名称,如:onclick
  643. // activeFun); // 事件触发的回调函数
  644. var iframe_ = EditorManager.getEditorFrame(name);
  645. iframe_.validationInfo = targetInput.validationInfo;
  646. iframe_.validationInfos = targetInput.validationInfos;
  647. iframe_.validationManager = targetInput.validationManager;
  648. onblurFun.call(iframe_);
  649. activeFun.call(iframe_);
  650. });
  651. });
  652. }
  653. // if(targetInput.tagName.toUpperCase()=='SELECT'){// 如果要校验的不是select元素
  654. // /** 为要校验的input元素绑定键盘点击事件* */
  655. // this.addEventListener(targetInput,// 需绑定监听事件的DOM元素
  656. // 'onkeyup',// 事件名称,如:onclick
  657. // activeFun);// 事件触发的回调函数
  658. // }else{
  659. //
  660. // /** 为要校验的input元素绑定键盘点击事件* */
  661. // this.addEventListener(targetInput,// 需绑定监听事件的DOM元素
  662. // 'onchange',// 事件名称,如:onclick
  663. // activeFun);// 事件触发的回调函数
  664. // }
  665. var eventType = null;
  666. //选择合适的时间为元素绑定
  667. //select
  668. if (targetInput.tagName == "SELECT") {
  669. eventType = "onchange";
  670. //input
  671. } else if (
  672. targetInput.tagName == "INPUT" ||
  673. targetInput.tagName == "TEXTAREA"
  674. ) {
  675. eventType = "onkeyup";
  676. if (targetInput.type == "button") {
  677. eventType = "onclick";
  678. }
  679. //button
  680. } else if (targetInput.tagName == "button") {
  681. eventType = "onclick";
  682. }
  683. if (eventType == null) {
  684. console.log(targetInput);
  685. console.log("元素不支检验");
  686. //alert("元素不支检验")
  687. return;
  688. }
  689. //为元素绑定即时校验事件
  690. this.addEventListener(
  691. targetInput, // 需绑定监听事件的DOM元素
  692. eventType, // 事件名称,如:onclick
  693. activeFun
  694. ); // 事件触发的回调函数
  695. }
  696. } // for
  697. //预校验,不用等到提交表单才校验 By Rd 2015-11-12
  698. this.checkAll(targetInput.validationInfos);
  699. // this.MsgDiv.style.top = "-500px"
  700. this.hideMsgDiv();
  701. } catch (e) {
  702. console.log(e);
  703. if (this.debug)
  704. //alert('方法initInput执行异常:'+e);
  705. console.error("方法initInput执行异常:" + e);
  706. }
  707. },
  708. /**
  709. * 为指定的表单元素绑定校验方法
  710. */
  711. add: function (
  712. validatorStr, // 内容为校验方法全路径的字符串
  713. formElems, // 被校验的表单元素的name或id
  714. argsObj, // 内容为当前校验器使用的参数组成的关联数组
  715. type
  716. ) {
  717. // 校验结果严重等级
  718. try {
  719. if (!type) type = ValidationManager.MSG_TYPE_ERROR;
  720. var validationInfo = {}; // 保存一次校验所需信息的集合
  721. validationInfo.funRef = this.getFunRef(validatorStr); // 校验器方法引用
  722. validationInfo.argsObj = argsObj; // 内容为当前校验器使用的参数组成的关联数组
  723. validationInfo.type = type; // 校验结果严重等级
  724. validationInfo.formElems = []; // 被校验的表单元素
  725. validationInfo.isNull = function () {
  726. return this.formElems[0].value ? false : true;
  727. };
  728. // validationInfo.formElems=this.getElements(eName);
  729. //查找表单元素
  730. for (var i = 0; i < formElems.length; i++) {
  731. var eName = formElems[i];
  732. if (window.growHeightList && window.growHeightList[eName]) {
  733. var gh = window.growHeightList[eName];
  734. var elems = [gh.getInputElement()];
  735. if (elems)
  736. validationInfo.formElems = validationInfo.formElems.concat(elems);
  737. } else {
  738. var elems = this.getElements(eName);
  739. if (elems)
  740. validationInfo.formElems = validationInfo.formElems.concat(elems);
  741. }
  742. }
  743. //bmxy1.xy
  744. //批量录入
  745. if (formElems[0].split(/\./).length == 2) {
  746. argsObj["namePrefix"] = formElems[0].split(/\./)[0] + ".";
  747. } else {
  748. argsObj["namePrefix"] = "";
  749. }
  750. validationInfo.name = formElems[0];
  751. // console.log(validationInfo)
  752. //只有找到对应表单元素时才绑定校验方法 张敏聪2015-10-30
  753. if (validationInfo.formElems.length > 0) {
  754. // 为指定的input元素绑定校验方法的公共方法
  755. this.initInput(validationInfo);
  756. }
  757. } catch (e) {
  758. if (this.debug) console.log("方法add执行异常:" + e);
  759. //alert('方法add执行异常:'+e);
  760. }
  761. },
  762. remove: function (
  763. validatorStr, // 校验器方法的全路径字符串
  764. formElems // 需要移除校验的表单元素的 name 或 id
  765. ) {
  766. try {
  767. // 获取校验方法引用
  768. var validationInfo = {
  769. funRef: this.getFunRef(validatorStr),
  770. formElems: []
  771. };
  772. // 查找表单元素
  773. for (var i = 0; i < formElems.length; i++) {
  774. var eName = formElems[i];
  775. var elems = this.getElements(eName);
  776. if (elems) {
  777. validationInfo.formElems = validationInfo.formElems.concat(elems);
  778. }
  779. }
  780. // 如果找到了对应的表单元素
  781. if (validationInfo.formElems.length > 0) {
  782. for (var j = 0; j < validationInfo.formElems.length; j++) {
  783. var elem = validationInfo.formElems[j];
  784. // 1. 移除所有校验相关事件绑定
  785. $(elem).off('input.validation keyup.validation change.validation focus.validation blur.validation');
  786. // 2. 移除校验提示信息(如错误消息)
  787. $(elem).removeClass('validation-error'); // 移除错误样式
  788. $(elem).next('.validation-error-msg').remove(); // 移除错误提示信息
  789. // 3. 恢复原始样式(例如,边框颜色)
  790. if (elem.oldBorderColor) {
  791. elem.style.borderBottomColor = elem.oldBorderColor;
  792. } else {
  793. elem.style.borderBottomColor = ''; // 如果没有保存原始样式,使用默认样式
  794. }
  795. // 4. 移除与必填项标记 `span` 无关的样式
  796. var spanId = elem.name + "Span"; // 拼接出对应的 span id,例如 "jfrxmSpan"
  797. var spanElem = document.getElementById(spanId);
  798. if (spanElem) {
  799. spanElem.remove(); // 直接删除这个 span 元素
  800. }
  801. // 5. 移除校验信息缓存
  802. this.validationInfos = this.validationInfos.filter(function(info) {
  803. return info.funRef !== validationInfo.funRef ||
  804. !info.formElems.some(elem => validationInfo.formElems.includes(elem));
  805. });
  806. // 6. 清理校验信息缓存
  807. if (elem.validationInfos) {
  808. elem.validationInfos = elem.validationInfos.filter(function(info) {
  809. return info.funRef !== validationInfo.funRef;
  810. });
  811. }
  812. }
  813. }
  814. } catch (e) {
  815. if (this.debug) console.log("方法 remove 执行异常: " + e);
  816. }
  817. }
  818. ,
  819. /**
  820. * 执行一次校验
  821. *
  822. * @param {}
  823. * validationInfo 保存一次校验的配置的对象
  824. * 单个校验
  825. */
  826. check: function (validationInfo, targetinput) {
  827. var rs = {}; // 保存校验结果的对象
  828. var funRef = validationInfo.funRef; // 校验器方法引用
  829. var argsObj = validationInfo.argsObj; // 内容为当前校验器使用的参数组成的关联数组
  830. var type = validationInfo.type; // 校验结果严重等级
  831. // console.log(targetinput.validationInfos)
  832. var funArgs = [];
  833. //2017-12-4 zhangmincong radio判断
  834. // if (targetinput.type && 'radio|checkbox'.indexOf(targetinput.type.toLowerCase()) > -1) {
  835. // var rdos = document.querySelectorAll("[type='" + targetinput.type + "'][name='" + targetinput.name + "']"),
  836. // value;
  837. // console.log(rdos)
  838. // for (var i = 0; i < rdos.length; i++) {
  839. // console.log(rdos[i].checked)
  840. // if (rdos[i].checked == true) {
  841. // value = rdos[i].value;
  842. // break;
  843. // }
  844. // }
  845. // funArgs.push([value]);
  846. // } else if ((typeof EditorManager != "undefined") && (argsObj.isUeditor == true || argsObj.isUeditor == "true")) {
  847. // if (EditorManager.getWDEditor(argsObj.editorName)) {
  848. // var value = EditorManager.getWDEditor(argsObj.editorName).getUeditor().getContent();
  849. // value = wd.display.toPlainText(value);
  850. // // console.log("ueditorValue:"+value);
  851. // funArgs.push([value]);
  852. // }
  853. // } else if (targetinput.tagName == "DIV") {
  854. // funArgs.push([targetinput.innerHTML])
  855. // } else if (targetinput.GrowHeight) {
  856. // funArgs.push([targetinput.GrowHeight.val()]);
  857. // } else {
  858. // funArgs.push([targetinput.value])
  859. // }
  860. // console.log(targetinput);
  861. // funArgs.push(argsObj)
  862. // var win = window;
  863. // var xy = this.recursionIframe(win);
  864. // 调用校验器进行校验
  865. var msg;
  866. try {
  867. // msg = funRef.apply(window, funArgs);
  868. //校验器获取根据name获得其他录入
  869. var environment = {
  870. argsObj: argsObj,
  871. getElementValue: function (name) {
  872. //单个或 批量 取值
  873. var n = this.argsObj.namePrefix + name;
  874. //id查找
  875. var ele = document.querySelector("input[id='" + n + "']");
  876. if (ele) return ele.value;
  877. //name查找
  878. var box = wd.display.getInputBox(n, true);
  879. return box ? box.getValue() : null;
  880. },
  881. setElementValue: function (name, value) {
  882. //单个或 批量 设值.....自动填写性别、出生地区、出生日期
  883. var n = this.argsObj.namePrefix + name;
  884. //name查找
  885. var box = wd.display.getInputBox(n, true);
  886. if (!box) {
  887. console.log("set value fail:" + n);
  888. return;
  889. }
  890. box.setValue(value);
  891. },
  892. modify: function (names, canModify) {
  893. //names:["xm","xbm"] 录入可修改,不可修改
  894. for (var i = 0; i < names.length; i++) {
  895. var n = this.argsObj.namePrefix + names[i];
  896. var box = wd.display.getInputBox(n);
  897. if (box) {
  898. box.modify(canModify);
  899. }
  900. }
  901. },
  902. };
  903. var vbox = wd.display.getInputBox(validationInfo.name, true);
  904. var vv = vbox ? vbox.getValue() : "";
  905. funArgs.push([vv]);
  906. funArgs.push(argsObj);
  907. msg = funRef.apply(environment, funArgs);
  908. } catch (e) {
  909. console.error(e);
  910. msg = "校验方法错误:" + validationInfo.funRef;
  911. }
  912. // 保存校验信息
  913. rs.msg = msg;
  914. rs.formElems = [targetinput];
  915. if (msg)
  916. // 如果校验不通过
  917. rs.type = type;
  918. else rs.type = ValidationManager.MSG_TYPE_SUCCESS;
  919. // console.log(rs);
  920. return rs;
  921. },
  922. checks: function (validationInfo, targetInput) {
  923. var result = [];
  924. if (targetInput) {
  925. var elem = targetInput;
  926. var rs = this.check(validationInfo, elem);
  927. //校验未通过
  928. if (rs.msg && rs.msg.length > 0) {
  929. result.push(rs);
  930. elem.style.borderBottomColor = "red";
  931. } else {
  932. //通过
  933. this.hideMsgDiv();
  934. // 如果运行到这里证明校验通过
  935. if (elem.oldBorderColor == "default") elem.style.borderBottomColor = "";
  936. else elem.style.borderBottomColor = elem.oldBorderColor;
  937. }
  938. } else {
  939. for (var i = 0; i < validationInfo.formElems.length; i++) {
  940. var elem = validationInfo.formElems[i];
  941. var rs = this.check(validationInfo, elem);
  942. // console.log(rs.msg)
  943. //校验未通过
  944. if (rs.msg && rs.msg.length > 0) {
  945. result.push(rs);
  946. elem.style.borderBottomColor = "red";
  947. } else {
  948. //通过
  949. this.hideMsgDiv();
  950. // 如果运行到这里证明校验通过
  951. if (elem.oldBorderColor == "default")
  952. elem.style.borderBottomColor = "";
  953. else elem.style.borderBottomColor = elem.oldBorderColor;
  954. }
  955. }
  956. }
  957. return result;
  958. },
  959. checkInput: function (targetinput) {
  960. var result = [];
  961. // targetinput里存有校验器
  962. if (targetinput.validationInfos) {
  963. var validationInfos = targetinput.validationInfos;
  964. for (var i = 0; i < validationInfos.length; i++) {
  965. var elem = targetinput;
  966. var rs = this.check(validationInfos[i], elem);
  967. //校验未通过
  968. if (rs.msg && rs.msg.length > 0) {
  969. result.push(rs);
  970. // elem.style.borderBottomColor='red';
  971. } else {
  972. //通过
  973. // this.hideMsgDiv()
  974. // // 如果运行到这里证明校验通过
  975. // if(elem.oldBorderColor=='default')
  976. // elem.style.borderBottomColor='';
  977. // else
  978. // elem.style.borderBottomColor=elem.oldBorderColor;
  979. }
  980. }
  981. }
  982. return result;
  983. },
  984. /**
  985. * 对数组中消息进行排序。
  986. */
  987. sortRs: function (a, b) {
  988. if (!a || !b) return -1;
  989. var typeA = a.type;
  990. var typeB = b.type;
  991. if (typeB == ValidationManager.MSG_TYPE_ERROR) {
  992. return 1;
  993. } else return -1;
  994. },
  995. /**
  996. * 获取表单元素的名字或ID
  997. */
  998. getNameOrId: function (formElem) {
  999. if (formElem.name) return formElem.name;
  1000. else return formElem.id;
  1001. },
  1002. /**
  1003. * 为保存到数组rsArr的消息,添加前缀。
  1004. */
  1005. addMsgPrefix: function (rsArr) {
  1006. for (var i = 0; i < rsArr.length; i++) {
  1007. var rs = rsArr[i];
  1008. var prefix;
  1009. if (rs.type == ValidationManager.MSG_TYPE_SUCCESS) {
  1010. prefix = "正确:";
  1011. } else if (rs.type == ValidationManager.MSG_TYPE_ERROR) {
  1012. prefix = "错误:";
  1013. } else if (rs.type == ValidationManager.MSG_TYPE_WARNING) {
  1014. prefix = "警告:";
  1015. }
  1016. rs.msg = prefix + rs.msg;
  1017. console.log(rs);
  1018. }
  1019. },
  1020. /**
  1021. * 检查所有要校验的input元素,并返回校验失败的结果
  1022. */
  1023. checkAll: function (validationInfos, targetInput) {
  1024. var rsArr = []; // 返回的校验结果
  1025. var allFormElems = []; // 保存所有要校验的表单元素
  1026. var errFormElems = {}; // 所有校验不通过的表单元素
  1027. for (var i = 0; i < validationInfos.length; i++) {
  1028. // 一个校验信息
  1029. var validationInfo = validationInfos[i];
  1030. /**
  1031. * 校验并获得校验结果 rs.msg = msg; rs.type = type; rs.formElems = formElems;
  1032. */
  1033. for (var j = 0; j < validationInfo.formElems.length; j++) {
  1034. var elem = validationInfo.formElems[j];
  1035. var rs = this.check(validationInfo, elem);
  1036. // console.log(rs.msg)
  1037. //校验未通过
  1038. if (rs.msg && rs.msg.length > 0) rsArr.push(rs);
  1039. this.via(elem, rs.msg);
  1040. // if (rs.msg && rs.msg.length > 0) {
  1041. // rsArr.push(rs)
  1042. // elem.style.borderBottomColor = 'red';
  1043. // this.checkObjectPicker(elem,rs.msg);
  1044. // } else { //通过
  1045. //
  1046. // // 如果运行到这里证明校验通过
  1047. // if (elem.oldBorderColor == 'default')
  1048. // elem.style.borderBottomColor = '';
  1049. // else
  1050. // elem.style.borderBottomColor = elem.oldBorderColor;
  1051. //
  1052. // this.checkObjectPicker(elem);
  1053. // this.hideMsgDiv()
  1054. // }
  1055. }
  1056. }
  1057. // 对消息进行排序
  1058. // rsArr.sort(this.sortRs);
  1059. // 为保存到数组rsArr的消息,添加前缀。
  1060. // this.addMsgPrefix(rsArr);
  1061. // console.log(rsArr)
  1062. return rsArr;
  1063. },
  1064. /**
  1065. * 如果校验通过,返回true
  1066. */
  1067. passValidation: function (rsArr) {
  1068. var rs = true;
  1069. if (rsArr && rsArr.length > 0) rs = false;
  1070. return rs;
  1071. },
  1072. /**
  1073. * 实时校验的方法
  1074. *
  1075. * @param {}
  1076. * targetInput目标input元素
  1077. */
  1078. checkForRealTime: function (targetInput) {
  1079. // try{
  1080. if (window.event && "focus" == window.event.type) {
  1081. // this.MsgDiv.style.top = "-500px"
  1082. this.hideMsgDiv();
  1083. return;
  1084. }
  1085. // 获得当前表单元素校验不通过的消息集
  1086. var rsArr = this.checkInput(targetInput);
  1087. var isnullFunction = false;
  1088. // if(targetInput.validationInfos&&targetInput.validationInfos.length==1){
  1089. //// console.log(targetInput.validationInfos[0].funRef==wd.commonValidator.notNull)
  1090. // isnullFunction=(targetInput.validationInfos[0].funRef==wd.commonValidator.notNull)
  1091. // }
  1092. // isnullFunction = !$(targetInput).val() // 去掉,导致鼠标进入时不显示错误信息 -- 不知干啥用???Lin
  1093. // console.log(targetInput.validationInfos);
  1094. if (window.event) {
  1095. if (window.event.type == "keyup") isnullFunction = false;
  1096. }
  1097. if (!isnullFunction) this.setMsg(rsArr); // 设定显示表单验证结果的iframe的显示内容
  1098. // console.log(rsArr);
  1099. this.via(targetInput, rsArr, isnullFunction);
  1100. //console.log(targetInput)
  1101. // 校验不通过,显示校验信息
  1102. // if (!this.passValidation(rsArr)) {
  1103. //// this.checkObjectPicker(targetInput,rsArr);
  1104. //// targetInput.style.borderBottomColor = "red"
  1105. // this.showMsgDiv(targetInput); // MsgDiv定位于要校验的目标input元素的上方
  1106. // } else { // 校验通过
  1107. //// this.checkObjectPicker(targetInput);
  1108. //
  1109. //// if (targetInput.oldBorderColor == 'default') {
  1110. //// targetInput.style.borderBottomColor = '';
  1111. //// } else {
  1112. //// targetInput.style.borderBottomColor = targetInput.oldBorderColor;
  1113. //// }
  1114. // this.hideMsgDiv(); // 隐藏MsgDiv
  1115. // }
  1116. return rsArr;
  1117. /*
  1118. * }catch(e){ if(this.debug) alert('方法checkForRealTime执行异常:'+e.message); }
  1119. */
  1120. },
  1121. recursionIframe: function (win) {
  1122. if (win.parent == top && win.parent == win) {
  1123. // var xy = this.getAbsPoint(win);
  1124. return {
  1125. x: 0,
  1126. y: 0,
  1127. w: 0,
  1128. h: 0,
  1129. };
  1130. }
  1131. var winPar = win.parent; // .opener
  1132. var iframeArr = winPar.document.getElementsByTagName("IFRAME");
  1133. var targetIframe;
  1134. for (var i = 0; i < iframeArr.length; i++) {
  1135. var iframeElem = iframeArr[i];
  1136. if (iframeElem.contentWindow == win) {
  1137. targetIframe = iframeElem;
  1138. break;
  1139. }
  1140. }
  1141. // alert(targetIframe==null)
  1142. var xy = this.getAbsPoint(targetIframe);
  1143. if (winPar.parent != winPar) {
  1144. var xy2 = this.recursionIframe(winPar);
  1145. xy.x += xy2.x;
  1146. xy.y += xy2.y;
  1147. }
  1148. return xy;
  1149. },
  1150. findObjectPickerInputer: function (target) {
  1151. var name = target.getAttribute("name");
  1152. if (!name) return;
  1153. var inputer = document.querySelector("[name=" + name + "Name]");
  1154. if (!inputer) {
  1155. inputer = document.querySelector("[name=" + name + "Input]");
  1156. if (!inputer && /id$/.test(name)) {
  1157. var inputName = name.replace(/id$/, "Name");
  1158. inputer = document.querySelector("[name=" + inputName + "]");
  1159. }
  1160. if (!inputer && /m$/.test(name)) {
  1161. var inputName = name.replace(/m$/, "Name");
  1162. inputer = document.querySelector("[name=" + inputName + "]");
  1163. }
  1164. }
  1165. return inputer;
  1166. },
  1167. checkObjectPicker: function (target, msg) {
  1168. var name = target.getAttribute("name");
  1169. var obj = wd.edit.objectPicker.getInstance(name);
  1170. if (!obj) return;
  1171. if (msg) {
  1172. obj.failValidation();
  1173. } else {
  1174. obj.passValidation();
  1175. }
  1176. // var inputer=this.findObjectPickerInputer(target);
  1177. // if(!inputer)return;
  1178. // if(msg){
  1179. // inputer.style.border="red 1px solid";
  1180. // }else{
  1181. // inputer.style.border="";
  1182. //
  1183. // }
  1184. },
  1185. via: function (targetInput, msg, isnullFunction) {
  1186. var gh = targetInput.GrowHeight;
  1187. var name = targetInput.getAttribute("name");
  1188. if (gh != null) {
  1189. name = targetInput.GrowHeight.getName();
  1190. }
  1191. if (!name || !wd.edit) return;
  1192. var leftSpan = document.getElementById(name + "Span");
  1193. if (!leftSpan) {
  1194. leftSpan = document.createElement("span");
  1195. leftSpan.setAttribute("id", name + "Span");
  1196. // leftSpan.setAttribute("class","validation-span");
  1197. leftSpan.setAttribute("class", "border-input-error vLine-leftWarning");
  1198. // leftSpan.style.borderLeft=" 1px solid red",
  1199. // leftSpan.style.height="100%";
  1200. // leftSpan.style.display= "none";
  1201. // leftSpan.style.position="absolute";
  1202. // leftSpan.style.left="0px";
  1203. }
  1204. if (gh != null) {
  1205. var growHeightElement = gh.getElement();
  1206. growHeightElement.parentNode.insertBefore(leftSpan, growHeightElement);
  1207. } else {
  1208. targetInput.parentNode.insertBefore(leftSpan, targetInput);
  1209. }
  1210. var obj;
  1211. try {
  1212. obj = wd.edit.objectPicker
  1213. ? wd.edit.objectPicker.getInstance(name)
  1214. : null;
  1215. } catch (e) {
  1216. console.log(e);
  1217. }
  1218. // // 获取或创建错误提示容器
  1219. // function getErrorContainer() {
  1220. // let errorContainer = document.getElementById("error-container");
  1221. // if (!errorContainer) {
  1222. // errorContainer = document.createElement("div");
  1223. // errorContainer.id = "error-container"; // 设置唯一ID
  1224. // errorContainer.style.cssText = `
  1225. // position: fixed;
  1226. // left: 0;
  1227. // right: 0;
  1228. // bottom: 0;
  1229. // text-align: center;
  1230. // background-color: #fff;
  1231. // padding: 10px;
  1232. // z-index: 1000;
  1233. // display: flex;
  1234. // font-size:1rem;
  1235. // color: red;
  1236. // justify-content: center;
  1237. // align-items: center;
  1238. // flex-direction: column;
  1239. // min-height: 50px; /* 限制为五条信息的高度 */
  1240. // overflow-y: auto; /* 超过五条信息时出现滚动条 */
  1241. // `;
  1242. // document.body.appendChild(errorContainer);
  1243. // }
  1244. // return errorContainer;
  1245. // }
  1246. // // 添加错误信息
  1247. // function addErrorMessage(name, msg) {
  1248. // const errorContainer = getErrorContainer();
  1249. // const errorId = `${name}-error`; // 基于 name 属性生成唯一的错误消息 ID
  1250. // let errorMessage = document.getElementById(errorId);
  1251. // if (!errorMessage) {
  1252. // // 如果不存在,创建新的错误消息
  1253. // errorMessage = document.createElement("div");
  1254. // errorMessage.id = errorId; // 给错误消息设置唯一 ID
  1255. // errorContainer.appendChild(errorMessage);
  1256. // }
  1257. // // 更新错误消息内容
  1258. // errorMessage.textContent = msg;
  1259. // }
  1260. // // 移除特定错误信息
  1261. // function removeErrorMessage(name) {
  1262. // const errorId = `${name}-error`;
  1263. // const errorMessage = document.getElementById(errorId);
  1264. // if (errorMessage) {
  1265. // errorMessage.remove(); // 删除对应的错误消息
  1266. // }
  1267. // }
  1268. // // 检查并移除错误容器
  1269. // function removeErrorContainerIfEmpty() {
  1270. // const errorContainer = document.getElementById("error-container");
  1271. // if (errorContainer && errorContainer.children.length === 0) {
  1272. // errorContainer.remove(); // 容器中无内容时移除
  1273. // }
  1274. // }
  1275. // 添加错误信息到指定的td元素中
  1276. function addErrorMessage(name, msg) {
  1277. const element = document.getElementsByName(name)[0]; // 获取 name 对应的元素
  1278. if (element) {
  1279. const tdElement = element.closest('td'); // 查找最近的父级 td 元素
  1280. if (tdElement) {
  1281. let errorMessage = tdElement.querySelector('.error-message'); // 查找是否已有错误信息元素
  1282. if (!errorMessage) {
  1283. // 如果不存在,创建一个新的错误信息元素
  1284. errorMessage = document.createElement("div");
  1285. errorMessage.className = "error-message"; // 设置一个类名,方便样式管理
  1286. errorMessage.style.cssText = `
  1287. position: absolute;
  1288. left: 0;
  1289. bottom: 0;
  1290. text-align: center;
  1291. color: red;
  1292. font-size:12px;
  1293. width: 100%;
  1294. text-align: right;
  1295. line-height: 18px;
  1296. height: 18px;
  1297. `;
  1298. tdElement.appendChild(errorMessage); // 将错误信息添加到 td 中
  1299. tdElement.style.borderBottom = "1px solid red";
  1300. }
  1301. errorMessage.textContent = msg; // 更新错误信息内容
  1302. }
  1303. }
  1304. }
  1305. // 移除特定错误信息
  1306. function removeErrorMessage(name) {
  1307. const element = document.getElementsByName(name)[0]; // 获取 name 对应的元素
  1308. if (element) {
  1309. const tdElement = element.closest('td'); // 查找最近的父级 td 元素
  1310. if (tdElement) {
  1311. const errorMessage = tdElement.querySelector('.error-message'); // 查找错误信息元素
  1312. if (errorMessage) {
  1313. errorMessage.remove(); // 删除错误信息元素
  1314. }
  1315. }
  1316. tdElement.style.borderBottom = "";
  1317. }
  1318. }
  1319. if (!this.passValidation(msg)) {
  1320. leftSpan.style.display = "";
  1321. if (msg && msg.length > 0 && msg[0].msg) {
  1322. if (!isnullFunction) {
  1323. if (obj) {
  1324. if(isMobile()){
  1325. addErrorMessage(obj, msg[0].msg); // 添加或更新错误消息
  1326. }else{
  1327. this.showMsgDiv(obj.getDisplayElement(), msg[0].msg);
  1328. }
  1329. } else {
  1330. // 如果是移动端,在底部显示错误信息,否则在气泡
  1331. if(isMobile()){
  1332. addErrorMessage(name, msg[0].msg); // 添加或更新错误消息
  1333. }else{
  1334. this.showMsgDiv(targetInput, msg[0].msg);
  1335. }
  1336. }
  1337. }
  1338. }
  1339. } else {
  1340. // 校验通过
  1341. removeErrorMessage(name); // 移除当前 name 对应的错误消息
  1342. //removeErrorContainerIfEmpty(); // 如果没有错误信息,移除容器
  1343. // this.checkObjectPicker(targetInput);
  1344. leftSpan.style.display = "none";
  1345. // targetInput.style.borderColor = '';
  1346. this.hideMsgDiv(); //隐藏MsgDiv;
  1347. }
  1348. },
  1349. mouseoverEvent: function (targetInput, activeFun) {
  1350. var THIS = this;
  1351. var target = targetInput;
  1352. var name = targetInput.getAttribute("name");
  1353. if (name && wd.edit && wd.edit.objectPicker) {
  1354. var obj = wd.edit.objectPicker.getInstance(name);
  1355. if (obj) {
  1356. target = obj.getDisplayElement();
  1357. }
  1358. }
  1359. var f = function () {
  1360. activeFun.call(targetInput);
  1361. };
  1362. $(target)
  1363. .on("mouseenter", f)
  1364. .on("mouseout", function () {
  1365. // console.log(window.event)
  1366. // if(window.event.srcElement!=target)return false
  1367. if ($(target).find(window.event.relatedTarget).length == 1)
  1368. return false;
  1369. // console.log(window.event.srcElement)
  1370. // THIS.MsgDiv.style.top = "-500px"
  1371. THIS.hideMsgDiv();
  1372. });
  1373. // this.addEventListener(target, // 需绑定监听事件的DOM元素
  1374. // 'onmouseenter', // 事件名称,如:onclick
  1375. // f); // 事件触发的回调函数
  1376. },
  1377. };