WebUIValidation.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * WebUIValidation.js
  3. *
  4. * Authors:
  5. * Chris Toshok ([email protected])
  6. *
  7. * (c) 2005 Novell, Inc. (http://www.novell.com)
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining
  10. * a copy of this software and associated documentation files (the
  11. * "Software"), to deal in the Software without restriction, including
  12. * without limitation the rights to use, copy, modify, merge, publish,
  13. * distribute, sublicense, and/or sell copies of the Software, and to
  14. * permit persons to whom the Software is furnished to do so, subject to
  15. * the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be
  18. * included in all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. var have_validation_summaries = false;
  29. function ValidatorOnLoad ()
  30. {
  31. if (typeof (Page_ValidationSummaries) != 'undefined' && Page_ValidationSummaries != null)
  32. have_validation_summaries = true;
  33. for (var v in Page_Validators) {
  34. var vo = Page_Validators [v];
  35. var funcname = vo.getAttribute ("evaluationfunction");
  36. func = this[funcname];
  37. vo["evaluationfunction"] = func;
  38. if (vo.getAttribute ("isvalid") == null)
  39. vo.setAttribute ("isvalid", "true");
  40. if (vo.getAttribute ("enabled") == null)
  41. vo.setAttribute ("enabled", "true");
  42. func = vo ["evaluationfunction"];
  43. }
  44. Page_ValidationActive = true;
  45. }
  46. var validation_result = true;
  47. function ValidatorCommonOnSubmit ()
  48. {
  49. /* handle validation summaries here */
  50. if (validation_result == false && have_validation_summaries) {
  51. for (var vi in Page_ValidationSummaries) {
  52. var vs = Page_ValidationSummaries[vi];
  53. var header = vs.getAttribute ("headertext");
  54. if (header == null)
  55. header = "";
  56. attr = vs.getAttribute ("showsummary");
  57. if (attr == null || attr.toLowerCase() == "true") {
  58. var displaymode = vs.getAttribute ("displaymode");
  59. if (displaymode == null) displaymode = "Bulleted";
  60. var html = "";
  61. if (displaymode == "List") {
  62. list_pre = "";
  63. list_post = "";
  64. item_pre = "";
  65. item_post = "<br>";
  66. }
  67. else if (displaymode == "SingleParagraph") {
  68. list_pre = "";
  69. list_post = "<br>";
  70. item_pre = "";
  71. item_post = " ";
  72. }
  73. else {
  74. list_pre = "<ul>";
  75. list_post = "</ul>";
  76. item_pre = "\n<li>";
  77. item_post = "</li>";
  78. }
  79. html += header;
  80. html += list_pre;
  81. for (var v in Page_Validators) {
  82. var vo = Page_Validators [v];
  83. if (vo.getAttribute ("isvalid").toLowerCase() == "false") {
  84. var text = ValidatorGetErrorMessage (vo);
  85. if (text != null && text != "") {
  86. html += item_pre + text + item_post;
  87. }
  88. }
  89. }
  90. html += list_post;
  91. vs.innerHTML = html;
  92. vs.style.display = "block";
  93. }
  94. attr = vs.getAttribute ("showmessagebox");
  95. if (attr != null && attr.toLowerCase() == "true") {
  96. var v_contents = "";
  97. for (var v in Page_Validators) {
  98. var vo = Page_Validators [v];
  99. if (vo.getAttribute ("isvalid").toLowerCase() == "false") {
  100. var text = ValidatorGetErrorMessage (vo);
  101. if (text != null && text != "") {
  102. v_contents += "-" + text + "\n";
  103. }
  104. }
  105. }
  106. var alert_header = header;
  107. if (alert_header != "")
  108. alert_header += "\n";
  109. summary_contents = alert_header + v_contents;
  110. alert (summary_contents);
  111. }
  112. }
  113. }
  114. rv = validation_result;
  115. validation_result = true;
  116. return rv;
  117. }
  118. function ValidatorGetValue (controlname)
  119. {
  120. var el = GetElement (controlname);
  121. /* if the element has a 'value' attribute, return it */
  122. if (typeof (el.value) != 'undefined' && el.value != null) {
  123. return el.value;
  124. }
  125. /* if it's a select, loop over the options looking for the
  126. * selected one. */
  127. if (typeof (el.selectedIndex) != 'undefined') {
  128. return el.options[el.selectedIndex].value;
  129. }
  130. }
  131. function ValidatorTrim (s)
  132. {
  133. s = s.replace (/^\s+/g, "");
  134. s = s.replace (/\s+$/g, "");
  135. return s;
  136. }
  137. function Page_ClientValidate()
  138. {
  139. validation_result = true;
  140. /* clear out the existing text from all our summaries */
  141. if (have_validation_summaries) {
  142. for (var vi in Page_ValidationSummaries) {
  143. var vs = Page_ValidationSummaries[vi];
  144. vs.style.display = "none";
  145. vs.innerHTML = "";
  146. }
  147. }
  148. for (var v in Page_Validators) {
  149. var vo = Page_Validators [v];
  150. var evalfunc = vo["evaluationfunction"];
  151. var result = false;
  152. if (vo.getAttribute ("enabled").toLowerCase() == "false") {
  153. result = true;
  154. ValidatorSucceeded (vo);
  155. }
  156. else {
  157. result = evalfunc (vo);
  158. }
  159. if (!result)
  160. validation_result = false;
  161. vo.setAttribute("isvalid", result ? "true" : "false");
  162. }
  163. return validation_result;
  164. }
  165. /*******************/
  166. /* type converters */
  167. function ToInteger (s)
  168. {
  169. if ((v = parseInt(s, 10)) != s - 0)
  170. return null;
  171. else
  172. return v;
  173. }
  174. function ToString (s)
  175. {
  176. return s;
  177. }
  178. function ToDouble (s)
  179. {
  180. if ((v = parseFloat(s)) != s - 0)
  181. return null;
  182. else
  183. return v;
  184. }
  185. function ToDate (s)
  186. {
  187. /* NYI */
  188. return null;
  189. }
  190. function ToCurrency (s)
  191. {
  192. /* NYI */
  193. return null;
  194. }
  195. /*******************/
  196. /* validators */
  197. function RangeValidatorEvaluateIsValid (validator)
  198. {
  199. var MinimumValue = parseInt (validator.getAttribute ("minimumvalue"));
  200. var MaximumValue = parseInt (validator.getAttribute ("maximumvalue"));
  201. var ControlToValidate = validator.getAttribute ("controltovalidate");
  202. var DataType = validator.getAttribute ("datatype");
  203. var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
  204. if (ctrl_value == "") {
  205. ValidatorSucceeded (validator);
  206. return true;
  207. }
  208. var val = Convert (ctrl_value, DataType);
  209. if (val == null || val < MinimumValue || val > MaximumValue) {
  210. ValidatorFailed (validator);
  211. return false;
  212. }
  213. else {
  214. ValidatorSucceeded (validator);
  215. return true;
  216. }
  217. }
  218. function RegularExpressionValidatorEvaluateIsValid (validator)
  219. {
  220. var ValidationExpression = validator.getAttribute ("validationexpression");
  221. var ControlToValidate = validator.getAttribute ("controltovalidate");
  222. var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
  223. if (ctrl_value == "") {
  224. ValidatorSucceeded (validator);
  225. return true;
  226. }
  227. var r = new RegExp (ValidationExpression);
  228. match = r.exec (ctrl_value);
  229. if (match == null || match[0] == "") {
  230. ValidatorFailed (validator);
  231. return false;
  232. }
  233. else {
  234. ValidatorSucceeded (validator);
  235. return true;
  236. }
  237. }
  238. function RequiredFieldValidatorEvaluateIsValid (validator)
  239. {
  240. var InitialValue = validator.getAttribute ("initialvalue");
  241. var ControlToValidate = validator.getAttribute ("controltovalidate");
  242. var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
  243. if (ctrl_value == ValidatorTrim (InitialValue)) {
  244. ValidatorFailed (validator);
  245. return false;
  246. }
  247. else {
  248. ValidatorSucceeded (validator);
  249. return true;
  250. }
  251. }
  252. function CustomValidatorEvaluateIsValid (validator)
  253. {
  254. var InitialValue = validator.getAttribute ("initialvalue");
  255. var ControlToValidate = validator.getAttribute ("controltovalidate");
  256. if (!ControlToValidate) {
  257. ValidatorSucceeded (validator);
  258. return true;
  259. }
  260. var evaluationfunc = validator.getAttribute ("clientvalidationfunction");
  261. var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
  262. var result = true;
  263. if (evaluationfunc && evaluationfunc != "") {
  264. args = {Value:ctrl_value, IsValid:false};
  265. eval (evaluationfunc + "(validator, args)");
  266. result = args.IsValid;
  267. }
  268. if (result) {
  269. ValidatorSucceeded (validator);
  270. return true;
  271. }
  272. else {
  273. ValidatorFailed (validator);
  274. return false;
  275. }
  276. }
  277. /*********************/
  278. /* utility functions */
  279. function Convert (s, ty)
  280. {
  281. var cvt = this ["To" + ty];
  282. if (typeof (cvt) == 'function')
  283. return cvt (s);
  284. else
  285. return null;
  286. }
  287. function ValidatorUpdateDisplay (v, valid)
  288. {
  289. var display = v.getAttribute ("display");
  290. /* for validators that aren't displayed, do nothing */
  291. if (display == "None") {
  292. return;
  293. }
  294. v.style.visibility = (valid ? "hidden" : "visible");
  295. if (display == "Dynamic") {
  296. v.style.display = (valid ? "none" : "inline");
  297. }
  298. }
  299. function ValidatorGetErrorMessage (v)
  300. {
  301. var text = v.getAttribute ("errormessage");
  302. if (text == null || text == "")
  303. text = v.getAttribute ("text");
  304. if (text == null)
  305. text = "";
  306. return text;
  307. }
  308. function ValidatorGetText (v)
  309. {
  310. var text = v.getAttribute ("text");
  311. if (text == null || text == "")
  312. text = v.getAttribute ("errormessage");
  313. if (text == null)
  314. text = "";
  315. return text;
  316. }
  317. function ValidatorFailed (v)
  318. {
  319. var text = ValidatorGetText (v);
  320. v.innerHTML = text;
  321. ValidatorUpdateDisplay (v, false);
  322. }
  323. function ValidatorSucceeded (v)
  324. {
  325. v.innerHTML = "";
  326. ValidatorUpdateDisplay (v, true);
  327. }
  328. function GetElement(id)
  329. {
  330. var x = document.getElementById ? document.getElementById (id) :
  331. ((document.all) ? document.all [id] : null);
  332. return x;
  333. }