WebUIValidation.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 CompareValidatorEvaluateIsValid (validator)
  198. {
  199. var ControlToCompare = validator.getAttribute ("controltocompare");
  200. var ValueToCompare = validator.getAttribute ("valuetocompare");
  201. var Operator = validator.getAttribute ("operator").toLowerCase();
  202. var ControlToValidate = validator.getAttribute ("controltovalidate");
  203. var DataType = validator.getAttribute ("datatype");
  204. var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
  205. var compare = (ControlToCompare != null && ControlToCompare != "") ? ValidatorTrim (ValidatorGetValue (ControlToCompare)) : ValueToCompare;
  206. var left = Convert (ctrl_value, DataType);
  207. if (left == null) {
  208. ValidatorFailed (validator);
  209. return false;
  210. }
  211. var right = Convert (compare, DataType);
  212. if (right == null) {
  213. ValidatorSucceeded (validator);
  214. return true;
  215. }
  216. var result = false;
  217. if (Operator == "equal") {
  218. result = (left == right);
  219. }
  220. else if (Operator == "notequal") {
  221. result = (left != right);
  222. }
  223. else if (Operator == "lessthan") {
  224. result = (left < right);
  225. }
  226. else if (Operator == "lessthanequal") {
  227. result = (left <= right);
  228. }
  229. else if (Operator == "greaterthan") {
  230. result = (left > right);
  231. }
  232. else if (Operator == "greaterthanequal") {
  233. result = (left >= right);
  234. }
  235. if (result == false) {
  236. ValidatorFailed (validator);
  237. return false;
  238. }
  239. else {
  240. ValidatorSucceeded (validator);
  241. return true;
  242. }
  243. }
  244. function RangeValidatorEvaluateIsValid (validator)
  245. {
  246. var MinimumValue = parseInt (validator.getAttribute ("minimumvalue"));
  247. var MaximumValue = parseInt (validator.getAttribute ("maximumvalue"));
  248. var ControlToValidate = validator.getAttribute ("controltovalidate");
  249. var DataType = validator.getAttribute ("datatype");
  250. var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
  251. if (ctrl_value == "") {
  252. ValidatorSucceeded (validator);
  253. return true;
  254. }
  255. var val = Convert (ctrl_value, DataType);
  256. if (val == null || val < MinimumValue || val > MaximumValue) {
  257. ValidatorFailed (validator);
  258. return false;
  259. }
  260. else {
  261. ValidatorSucceeded (validator);
  262. return true;
  263. }
  264. }
  265. function RegularExpressionValidatorEvaluateIsValid (validator)
  266. {
  267. var ValidationExpression = validator.getAttribute ("validationexpression");
  268. var ControlToValidate = validator.getAttribute ("controltovalidate");
  269. var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
  270. if (ctrl_value == "") {
  271. ValidatorSucceeded (validator);
  272. return true;
  273. }
  274. var r = new RegExp (ValidationExpression);
  275. match = r.exec (ctrl_value);
  276. if (match == null || match[0] == "") {
  277. ValidatorFailed (validator);
  278. return false;
  279. }
  280. else {
  281. ValidatorSucceeded (validator);
  282. return true;
  283. }
  284. }
  285. function RequiredFieldValidatorEvaluateIsValid (validator)
  286. {
  287. var InitialValue = validator.getAttribute ("initialvalue");
  288. var ControlToValidate = validator.getAttribute ("controltovalidate");
  289. var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
  290. if (ctrl_value == ValidatorTrim (InitialValue)) {
  291. ValidatorFailed (validator);
  292. return false;
  293. }
  294. else {
  295. ValidatorSucceeded (validator);
  296. return true;
  297. }
  298. }
  299. function CustomValidatorEvaluateIsValid (validator)
  300. {
  301. var InitialValue = validator.getAttribute ("initialvalue");
  302. var ControlToValidate = validator.getAttribute ("controltovalidate");
  303. if (!ControlToValidate) {
  304. ValidatorSucceeded (validator);
  305. return true;
  306. }
  307. var evaluationfunc = validator.getAttribute ("clientvalidationfunction");
  308. var ctrl_value = ValidatorTrim (ValidatorGetValue (ControlToValidate));
  309. var result = true;
  310. if (evaluationfunc && evaluationfunc != "") {
  311. args = {Value:ctrl_value, IsValid:false};
  312. eval (evaluationfunc + "(validator, args)");
  313. result = args.IsValid;
  314. }
  315. if (result) {
  316. ValidatorSucceeded (validator);
  317. return true;
  318. }
  319. else {
  320. ValidatorFailed (validator);
  321. return false;
  322. }
  323. }
  324. /*********************/
  325. /* utility functions */
  326. function Convert (s, ty)
  327. {
  328. var cvt = this ["To" + ty];
  329. if (typeof (cvt) == 'function')
  330. return cvt (s);
  331. else
  332. return null;
  333. }
  334. function ValidatorUpdateDisplay (v, valid)
  335. {
  336. var display = v.getAttribute ("display");
  337. /* for validators that aren't displayed, do nothing */
  338. if (display == "None") {
  339. return;
  340. }
  341. v.style.visibility = (valid ? "hidden" : "visible");
  342. if (display == "Dynamic") {
  343. v.style.display = (valid ? "none" : "inline");
  344. }
  345. }
  346. function ValidatorGetErrorMessage (v)
  347. {
  348. var text = v.getAttribute ("errormessage");
  349. if (text == null || text == "")
  350. text = v.getAttribute ("text");
  351. if (text == null)
  352. text = "";
  353. return text;
  354. }
  355. function ValidatorGetText (v)
  356. {
  357. var text = v.getAttribute ("text");
  358. if (text == null || text == "")
  359. text = v.getAttribute ("errormessage");
  360. if (text == null)
  361. text = "";
  362. return text;
  363. }
  364. function ValidatorFailed (v)
  365. {
  366. var text = ValidatorGetText (v);
  367. v.innerHTML = text;
  368. ValidatorUpdateDisplay (v, false);
  369. }
  370. function ValidatorSucceeded (v)
  371. {
  372. v.innerHTML = "";
  373. ValidatorUpdateDisplay (v, true);
  374. }
  375. function GetElement(id)
  376. {
  377. var x = document.getElementById ? document.getElementById (id) :
  378. ((document.all) ? document.all [id] : null);
  379. return x;
  380. }