WebUIValidation.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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;
  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. return validation_result;
  115. }
  116. function ValidatorGetValue (controlname)
  117. {
  118. var el = GetElement (controlname);
  119. /* if the element has a 'value' attribute, return it */
  120. if (typeof (el.value) != 'undefined' && el.value != null) {
  121. return el.value;
  122. }
  123. /* if it's a select, loop over the options looking for the
  124. * selected one. */
  125. if (typeof (el.selectedIndex) != 'undefined') {
  126. return el.options[el.selectedIndex].value;
  127. }
  128. }
  129. function ValidatorTrim (s)
  130. {
  131. s = s.replace (/^\s+/g, "");
  132. s = s.replace (/\s+$/g, "");
  133. return s;
  134. }
  135. function Page_ClientValidate()
  136. {
  137. validation_result = true;
  138. /* clear out the existing text from all our summaries */
  139. if (have_validation_summaries) {
  140. for (var vi in Page_ValidationSummaries) {
  141. var vs = Page_ValidationSummaries[vi];
  142. vs.style.display = "none";
  143. vs.innerHTML = "";
  144. }
  145. }
  146. for (var v in Page_Validators) {
  147. var vo = Page_Validators [v];
  148. var evalfunc = vo["evaluationfunction"];
  149. var result = false;
  150. if (vo.getAttribute ("enabled").toLowerCase() == "false") {
  151. result = true;
  152. ValidatorSucceeded (vo);
  153. }
  154. else {
  155. result = evalfunc (vo);
  156. }
  157. if (!result)
  158. validation_result = false;
  159. vo.setAttribute("isvalid", result ? "true" : "false");
  160. }
  161. return validation_result;
  162. }
  163. /*******************/
  164. /* type converters */
  165. function ToInteger (s)
  166. {
  167. if ((v = parseInt(s, 10)) != s - 0)
  168. return null;
  169. else
  170. return v;
  171. }
  172. function ToString (s)
  173. {
  174. return s;
  175. }
  176. function ToDouble (s)
  177. {
  178. if ((v = parseFloat(s)) != s - 0)
  179. return null;
  180. else
  181. return v;
  182. }
  183. function ToDate (s)
  184. {
  185. /* NYI */
  186. return null;
  187. }
  188. function ToCurrency (s)
  189. {
  190. /* NYI */
  191. return null;
  192. }
  193. /*******************/
  194. /* validators */
  195. function RangeValidatorEvaluateIsValid (validator)
  196. {
  197. var MinimumValue = parseInt (validator.getAttribute ("minimumvalue"));
  198. var MaximumValue = parseInt (validator.getAttribute ("maximumvalue"));
  199. var ControlToValidate = validator.getAttribute ("controltovalidate");
  200. var DataType = validator.getAttribute ("datatype");
  201. var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
  202. if (ctrl_value == "") {
  203. ValidatorSucceeded (validator);
  204. return true;
  205. }
  206. var val = Convert (ctrl_value, DataType);
  207. if (val == null || val < MinimumValue || val > MaximumValue) {
  208. ValidatorFailed (validator);
  209. return false;
  210. }
  211. else {
  212. ValidatorSucceeded (validator);
  213. return true;
  214. }
  215. }
  216. function RegularExpressionValidatorEvaluateIsValid (validator)
  217. {
  218. var ValidationExpression = validator.getAttribute ("validationexpression");
  219. var ControlToValidate = validator.getAttribute ("controltovalidate");
  220. var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
  221. if (ctrl_value == "") {
  222. ValidatorSucceeded (validator);
  223. return true;
  224. }
  225. var r = new RegExp (ValidationExpression);
  226. match = r.exec (ctrl_value);
  227. if (match == null || match[0] == "") {
  228. ValidatorFailed (validator);
  229. return false;
  230. }
  231. else {
  232. ValidatorSucceeded (validator);
  233. return true;
  234. }
  235. }
  236. function RequiredFieldValidatorEvaluateIsValid (validator)
  237. {
  238. var InitialValue = validator.getAttribute ("initialvalue");
  239. var ControlToValidate = validator.getAttribute ("controltovalidate");
  240. var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
  241. if (ctrl_value == ValidatorTrim (InitialValue)) {
  242. ValidatorFailed (validator);
  243. return false;
  244. }
  245. else {
  246. ValidatorSucceeded (validator);
  247. return true;
  248. }
  249. }
  250. function CustomValidatorEvaluateIsValid (validator)
  251. {
  252. var InitialValue = validator.getAttribute ("initialvalue");
  253. var ControlToValidate = validator.getAttribute ("controltovalidate");
  254. var evaluationfunc = validator.getAttribute ("clientvalidationfunction");
  255. var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
  256. var result = false;
  257. if (evaluationfunc && evaluationfunc != "") {
  258. args = {Value:ctrl_value, IsValid:false};
  259. eval (evaluationfunc + "(validator, args)");
  260. result = args.IsValid;
  261. }
  262. if (result) {
  263. ValidatorSucceeded (validator);
  264. return true;
  265. }
  266. else {
  267. ValidatorFailed (validator);
  268. return false;
  269. }
  270. }
  271. /*********************/
  272. /* utility functions */
  273. function Convert (s, ty)
  274. {
  275. var cvt = this ["To" + ty];
  276. if (typeof (cvt) == 'function')
  277. return cvt (s);
  278. else
  279. return null;
  280. }
  281. function ValidatorUpdateDisplay (v, valid)
  282. {
  283. var display = v.getAttribute ("display");
  284. /* for validators that aren't displayed, do nothing */
  285. if (display == "None") {
  286. return;
  287. }
  288. v.style.visibility = (valid ? "hidden" : "visible");
  289. if (display == "Dynamic") {
  290. v.style.display = (valid ? "none" : "inline");
  291. }
  292. }
  293. function ValidatorGetErrorMessage (v)
  294. {
  295. var text = v.getAttribute ("errormessage");
  296. if (text == null || text == "")
  297. text = v.getAttribute ("text");
  298. if (text == null)
  299. text = "";
  300. return text;
  301. }
  302. function ValidatorGetText (v)
  303. {
  304. var text = v.getAttribute ("text");
  305. if (text == null || text == "")
  306. text = v.getAttribute ("errormessage");
  307. if (text == null)
  308. text = "";
  309. return text;
  310. }
  311. function ValidatorFailed (v)
  312. {
  313. var text = ValidatorGetText (v);
  314. v.innerHTML = text;
  315. ValidatorUpdateDisplay (v, false);
  316. }
  317. function ValidatorSucceeded (v)
  318. {
  319. v.innerHTML = "";
  320. ValidatorUpdateDisplay (v, false);
  321. }
  322. function GetElement(id)
  323. {
  324. var x = document.getElementById ? document.getElementById (id) :
  325. ((document.all) ? document.all [id] : null);
  326. return x;
  327. }