PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/editors/tiny_mce_3_4_3_1/utils/validate.js

#
JavaScript | 252 lines | 176 code | 54 blank | 22 comment | 66 complexity | bfbaf5dc8058ff772afe2d87f4e82fbf MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. /**
  2. * validate.js
  3. *
  4. * Copyright 2009, Moxiecode Systems AB
  5. * Released under LGPL License.
  6. *
  7. * License: http://tinymce.moxiecode.com/license
  8. * Contributing: http://tinymce.moxiecode.com/contributing
  9. */
  10. /**
  11. // String validation:
  12. if (!Validator.isEmail('myemail'))
  13. alert('Invalid email.');
  14. // Form validation:
  15. var f = document.forms['myform'];
  16. if (!Validator.isEmail(f.myemail))
  17. alert('Invalid email.');
  18. */
  19. var Validator = {
  20. isEmail : function(s) {
  21. return this.test(s, '^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+@[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$');
  22. },
  23. isAbsUrl : function(s) {
  24. return this.test(s, '^(news|telnet|nttp|file|http|ftp|https)://[-A-Za-z0-9\\.]+\\/?.*$');
  25. },
  26. isSize : function(s) {
  27. return this.test(s, '^[0-9.]+(%|in|cm|mm|em|ex|pt|pc|px)?$');
  28. },
  29. isId : function(s) {
  30. return this.test(s, '^[A-Za-z_]([A-Za-z0-9_])*$');
  31. },
  32. isEmpty : function(s) {
  33. var nl, i;
  34. if (s.nodeName == 'SELECT' && s.selectedIndex < 1)
  35. return true;
  36. if (s.type == 'checkbox' && !s.checked)
  37. return true;
  38. if (s.type == 'radio') {
  39. for (i=0, nl = s.form.elements; i<nl.length; i++) {
  40. if (nl[i].type == "radio" && nl[i].name == s.name && nl[i].checked)
  41. return false;
  42. }
  43. return true;
  44. }
  45. return new RegExp('^\\s*$').test(s.nodeType == 1 ? s.value : s);
  46. },
  47. isNumber : function(s, d) {
  48. return !isNaN(s.nodeType == 1 ? s.value : s) && (!d || !this.test(s, '^-?[0-9]*\\.[0-9]*$'));
  49. },
  50. test : function(s, p) {
  51. s = s.nodeType == 1 ? s.value : s;
  52. return s == '' || new RegExp(p).test(s);
  53. }
  54. };
  55. var AutoValidator = {
  56. settings : {
  57. id_cls : 'id',
  58. int_cls : 'int',
  59. url_cls : 'url',
  60. number_cls : 'number',
  61. email_cls : 'email',
  62. size_cls : 'size',
  63. required_cls : 'required',
  64. invalid_cls : 'invalid',
  65. min_cls : 'min',
  66. max_cls : 'max'
  67. },
  68. init : function(s) {
  69. var n;
  70. for (n in s)
  71. this.settings[n] = s[n];
  72. },
  73. validate : function(f) {
  74. var i, nl, s = this.settings, c = 0;
  75. nl = this.tags(f, 'label');
  76. for (i=0; i<nl.length; i++) {
  77. this.removeClass(nl[i], s.invalid_cls);
  78. nl[i].setAttribute('aria-invalid', false);
  79. }
  80. c += this.validateElms(f, 'input');
  81. c += this.validateElms(f, 'select');
  82. c += this.validateElms(f, 'textarea');
  83. return c == 3;
  84. },
  85. invalidate : function(n) {
  86. this.mark(n.form, n);
  87. },
  88. getErrorMessages : function(f) {
  89. var nl, i, s = this.settings, field, msg, values, messages = [], ed = tinyMCEPopup.editor;
  90. nl = this.tags(f, "label");
  91. for (i=0; i<nl.length; i++) {
  92. if (this.hasClass(nl[i], s.invalid_cls)) {
  93. field = document.getElementById(nl[i].getAttribute("for"));
  94. values = { field: nl[i].textContent };
  95. if (this.hasClass(field, s.min_cls, true)) {
  96. message = ed.getLang('invalid_data_min');
  97. values.min = this.getNum(field, s.min_cls);
  98. } else if (this.hasClass(field, s.number_cls)) {
  99. message = ed.getLang('invalid_data_number');
  100. } else if (this.hasClass(field, s.size_cls)) {
  101. message = ed.getLang('invalid_data_size');
  102. } else {
  103. message = ed.getLang('invalid_data');
  104. }
  105. message = message.replace(/{\#([^}]+)\}/g, function(a, b) {
  106. return values[b] || '{#' + b + '}';
  107. });
  108. messages.push(message);
  109. }
  110. }
  111. return messages;
  112. },
  113. reset : function(e) {
  114. var t = ['label', 'input', 'select', 'textarea'];
  115. var i, j, nl, s = this.settings;
  116. if (e == null)
  117. return;
  118. for (i=0; i<t.length; i++) {
  119. nl = this.tags(e.form ? e.form : e, t[i]);
  120. for (j=0; j<nl.length; j++) {
  121. this.removeClass(nl[j], s.invalid_cls);
  122. nl[j].setAttribute('aria-invalid', false);
  123. }
  124. }
  125. },
  126. validateElms : function(f, e) {
  127. var nl, i, n, s = this.settings, st = true, va = Validator, v;
  128. nl = this.tags(f, e);
  129. for (i=0; i<nl.length; i++) {
  130. n = nl[i];
  131. this.removeClass(n, s.invalid_cls);
  132. if (this.hasClass(n, s.required_cls) && va.isEmpty(n))
  133. st = this.mark(f, n);
  134. if (this.hasClass(n, s.number_cls) && !va.isNumber(n))
  135. st = this.mark(f, n);
  136. if (this.hasClass(n, s.int_cls) && !va.isNumber(n, true))
  137. st = this.mark(f, n);
  138. if (this.hasClass(n, s.url_cls) && !va.isAbsUrl(n))
  139. st = this.mark(f, n);
  140. if (this.hasClass(n, s.email_cls) && !va.isEmail(n))
  141. st = this.mark(f, n);
  142. if (this.hasClass(n, s.size_cls) && !va.isSize(n))
  143. st = this.mark(f, n);
  144. if (this.hasClass(n, s.id_cls) && !va.isId(n))
  145. st = this.mark(f, n);
  146. if (this.hasClass(n, s.min_cls, true)) {
  147. v = this.getNum(n, s.min_cls);
  148. if (isNaN(v) || parseInt(n.value) < parseInt(v))
  149. st = this.mark(f, n);
  150. }
  151. if (this.hasClass(n, s.max_cls, true)) {
  152. v = this.getNum(n, s.max_cls);
  153. if (isNaN(v) || parseInt(n.value) > parseInt(v))
  154. st = this.mark(f, n);
  155. }
  156. }
  157. return st;
  158. },
  159. hasClass : function(n, c, d) {
  160. return new RegExp('\\b' + c + (d ? '[0-9]+' : '') + '\\b', 'g').test(n.className);
  161. },
  162. getNum : function(n, c) {
  163. c = n.className.match(new RegExp('\\b' + c + '([0-9]+)\\b', 'g'))[0];
  164. c = c.replace(/[^0-9]/g, '');
  165. return c;
  166. },
  167. addClass : function(n, c, b) {
  168. var o = this.removeClass(n, c);
  169. n.className = b ? c + (o != '' ? (' ' + o) : '') : (o != '' ? (o + ' ') : '') + c;
  170. },
  171. removeClass : function(n, c) {
  172. c = n.className.replace(new RegExp("(^|\\s+)" + c + "(\\s+|$)"), ' ');
  173. return n.className = c != ' ' ? c : '';
  174. },
  175. tags : function(f, s) {
  176. return f.getElementsByTagName(s);
  177. },
  178. mark : function(f, n) {
  179. var s = this.settings;
  180. this.addClass(n, s.invalid_cls);
  181. n.setAttribute('aria-invalid', 'true');
  182. this.markLabels(f, n, s.invalid_cls);
  183. return false;
  184. },
  185. markLabels : function(f, n, ic) {
  186. var nl, i;
  187. nl = this.tags(f, "label");
  188. for (i=0; i<nl.length; i++) {
  189. if (nl[i].getAttribute("for") == n.id || nl[i].htmlFor == n.id)
  190. this.addClass(nl[i], ic);
  191. }
  192. return null;
  193. }
  194. };