A QCodo powered CMS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
4.9 KiB

  1. /**
  2. * $Id: validate.js 422 2008-12-12 18:28:35Z erikwinn $
  3. *
  4. * Various form validation methods.
  5. *
  6. * @author Moxiecode
  7. * @copyright Copyright 2004-2008, Moxiecode Systems AB, All rights reserved.
  8. */
  9. /**
  10. // String validation:
  11. if (!Validator.isEmail('myemail'))
  12. alert('Invalid email.');
  13. // Form validation:
  14. var f = document.forms['myform'];
  15. if (!Validator.isEmail(f.myemail))
  16. alert('Invalid email.');
  17. */
  18. var Validator = {
  19. isEmail : function(s) {
  20. return this.test(s, '^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+@[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$');
  21. },
  22. isAbsUrl : function(s) {
  23. return this.test(s, '^(news|telnet|nttp|file|http|ftp|https)://[-A-Za-z0-9\\.]+\\/?.*$');
  24. },
  25. isSize : function(s) {
  26. return this.test(s, '^[0-9]+(%|in|cm|mm|em|ex|pt|pc|px)?$');
  27. },
  28. isId : function(s) {
  29. return this.test(s, '^[A-Za-z_]([A-Za-z0-9_])*$');
  30. },
  31. isEmpty : function(s) {
  32. var nl, i;
  33. if (s.nodeName == 'SELECT' && s.selectedIndex < 1)
  34. return true;
  35. if (s.type == 'checkbox' && !s.checked)
  36. return true;
  37. if (s.type == 'radio') {
  38. for (i=0, nl = s.form.elements; i<nl.length; i++) {
  39. if (nl[i].type == "radio" && nl[i].name == s.name && nl[i].checked)
  40. return false;
  41. }
  42. return true;
  43. }
  44. return new RegExp('^\\s*$').test(s.nodeType == 1 ? s.value : s);
  45. },
  46. isNumber : function(s, d) {
  47. return !isNaN(s.nodeType == 1 ? s.value : s) && (!d || !this.test(s, '^-?[0-9]*\\.[0-9]*$'));
  48. },
  49. test : function(s, p) {
  50. s = s.nodeType == 1 ? s.value : s;
  51. return s == '' || new RegExp(p).test(s);
  52. }
  53. };
  54. var AutoValidator = {
  55. settings : {
  56. id_cls : 'id',
  57. int_cls : 'int',
  58. url_cls : 'url',
  59. number_cls : 'number',
  60. email_cls : 'email',
  61. size_cls : 'size',
  62. required_cls : 'required',
  63. invalid_cls : 'invalid',
  64. min_cls : 'min',
  65. max_cls : 'max'
  66. },
  67. init : function(s) {
  68. var n;
  69. for (n in s)
  70. this.settings[n] = s[n];
  71. },
  72. validate : function(f) {
  73. var i, nl, s = this.settings, c = 0;
  74. nl = this.tags(f, 'label');
  75. for (i=0; i<nl.length; i++)
  76. this.removeClass(nl[i], s.invalid_cls);
  77. c += this.validateElms(f, 'input');
  78. c += this.validateElms(f, 'select');
  79. c += this.validateElms(f, 'textarea');
  80. return c == 3;
  81. },
  82. invalidate : function(n) {
  83. this.mark(n.form, n);
  84. },
  85. reset : function(e) {
  86. var t = ['label', 'input', 'select', 'textarea'];
  87. var i, j, nl, s = this.settings;
  88. if (e == null)
  89. return;
  90. for (i=0; i<t.length; i++) {
  91. nl = this.tags(e.form ? e.form : e, t[i]);
  92. for (j=0; j<nl.length; j++)
  93. this.removeClass(nl[j], s.invalid_cls);
  94. }
  95. },
  96. validateElms : function(f, e) {
  97. var nl, i, n, s = this.settings, st = true, va = Validator, v;
  98. nl = this.tags(f, e);
  99. for (i=0; i<nl.length; i++) {
  100. n = nl[i];
  101. this.removeClass(n, s.invalid_cls);
  102. if (this.hasClass(n, s.required_cls) && va.isEmpty(n))
  103. st = this.mark(f, n);
  104. if (this.hasClass(n, s.number_cls) && !va.isNumber(n))
  105. st = this.mark(f, n);
  106. if (this.hasClass(n, s.int_cls) && !va.isNumber(n, true))
  107. st = this.mark(f, n);
  108. if (this.hasClass(n, s.url_cls) && !va.isAbsUrl(n))
  109. st = this.mark(f, n);
  110. if (this.hasClass(n, s.email_cls) && !va.isEmail(n))
  111. st = this.mark(f, n);
  112. if (this.hasClass(n, s.size_cls) && !va.isSize(n))
  113. st = this.mark(f, n);
  114. if (this.hasClass(n, s.id_cls) && !va.isId(n))
  115. st = this.mark(f, n);
  116. if (this.hasClass(n, s.min_cls, true)) {
  117. v = this.getNum(n, s.min_cls);
  118. if (isNaN(v) || parseInt(n.value) < parseInt(v))
  119. st = this.mark(f, n);
  120. }
  121. if (this.hasClass(n, s.max_cls, true)) {
  122. v = this.getNum(n, s.max_cls);
  123. if (isNaN(v) || parseInt(n.value) > parseInt(v))
  124. st = this.mark(f, n);
  125. }
  126. }
  127. return st;
  128. },
  129. hasClass : function(n, c, d) {
  130. return new RegExp('\\b' + c + (d ? '[0-9]+' : '') + '\\b', 'g').test(n.className);
  131. },
  132. getNum : function(n, c) {
  133. c = n.className.match(new RegExp('\\b' + c + '([0-9]+)\\b', 'g'))[0];
  134. c = c.replace(/[^0-9]/g, '');
  135. return c;
  136. },
  137. addClass : function(n, c, b) {
  138. var o = this.removeClass(n, c);
  139. n.className = b ? c + (o != '' ? (' ' + o) : '') : (o != '' ? (o + ' ') : '') + c;
  140. },
  141. removeClass : function(n, c) {
  142. c = n.className.replace(new RegExp("(^|\\s+)" + c + "(\\s+|$)"), ' ');
  143. return n.className = c != ' ' ? c : '';
  144. },
  145. tags : function(f, s) {
  146. return f.getElementsByTagName(s);
  147. },
  148. mark : function(f, n) {
  149. var s = this.settings;
  150. this.addClass(n, s.invalid_cls);
  151. this.markLabels(f, n, s.invalid_cls);
  152. return false;
  153. },
  154. markLabels : function(f, n, ic) {
  155. var nl, i;
  156. nl = this.tags(f, "label");
  157. for (i=0; i<nl.length; i++) {
  158. if (nl[i].getAttribute("for") == n.id || nl[i].htmlFor == n.id)
  159. this.addClass(nl[i], ic);
  160. }
  161. return null;
  162. }
  163. };