25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

221 lines
4.9 KiB

  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. c += this.validateElms(f, 'input');
  79. c += this.validateElms(f, 'select');
  80. c += this.validateElms(f, 'textarea');
  81. return c == 3;
  82. },
  83. invalidate : function(n) {
  84. this.mark(n.form, n);
  85. },
  86. reset : function(e) {
  87. var t = ['label', 'input', 'select', 'textarea'];
  88. var i, j, nl, s = this.settings;
  89. if (e == null)
  90. return;
  91. for (i=0; i<t.length; i++) {
  92. nl = this.tags(e.form ? e.form : e, t[i]);
  93. for (j=0; j<nl.length; j++)
  94. this.removeClass(nl[j], s.invalid_cls);
  95. }
  96. },
  97. validateElms : function(f, e) {
  98. var nl, i, n, s = this.settings, st = true, va = Validator, v;
  99. nl = this.tags(f, e);
  100. for (i=0; i<nl.length; i++) {
  101. n = nl[i];
  102. this.removeClass(n, s.invalid_cls);
  103. if (this.hasClass(n, s.required_cls) && va.isEmpty(n))
  104. st = this.mark(f, n);
  105. if (this.hasClass(n, s.number_cls) && !va.isNumber(n))
  106. st = this.mark(f, n);
  107. if (this.hasClass(n, s.int_cls) && !va.isNumber(n, true))
  108. st = this.mark(f, n);
  109. if (this.hasClass(n, s.url_cls) && !va.isAbsUrl(n))
  110. st = this.mark(f, n);
  111. if (this.hasClass(n, s.email_cls) && !va.isEmail(n))
  112. st = this.mark(f, n);
  113. if (this.hasClass(n, s.size_cls) && !va.isSize(n))
  114. st = this.mark(f, n);
  115. if (this.hasClass(n, s.id_cls) && !va.isId(n))
  116. st = this.mark(f, n);
  117. if (this.hasClass(n, s.min_cls, true)) {
  118. v = this.getNum(n, s.min_cls);
  119. if (isNaN(v) || parseInt(n.value) < parseInt(v))
  120. st = this.mark(f, n);
  121. }
  122. if (this.hasClass(n, s.max_cls, true)) {
  123. v = this.getNum(n, s.max_cls);
  124. if (isNaN(v) || parseInt(n.value) > parseInt(v))
  125. st = this.mark(f, n);
  126. }
  127. }
  128. return st;
  129. },
  130. hasClass : function(n, c, d) {
  131. return new RegExp('\\b' + c + (d ? '[0-9]+' : '') + '\\b', 'g').test(n.className);
  132. },
  133. getNum : function(n, c) {
  134. c = n.className.match(new RegExp('\\b' + c + '([0-9]+)\\b', 'g'))[0];
  135. c = c.replace(/[^0-9]/g, '');
  136. return c;
  137. },
  138. addClass : function(n, c, b) {
  139. var o = this.removeClass(n, c);
  140. n.className = b ? c + (o != '' ? (' ' + o) : '') : (o != '' ? (o + ' ') : '') + c;
  141. },
  142. removeClass : function(n, c) {
  143. c = n.className.replace(new RegExp("(^|\\s+)" + c + "(\\s+|$)"), ' ');
  144. return n.className = c != ' ' ? c : '';
  145. },
  146. tags : function(f, s) {
  147. return f.getElementsByTagName(s);
  148. },
  149. mark : function(f, n) {
  150. var s = this.settings;
  151. this.addClass(n, s.invalid_cls);
  152. this.markLabels(f, n, s.invalid_cls);
  153. return false;
  154. },
  155. markLabels : function(f, n, ic) {
  156. var nl, i;
  157. nl = this.tags(f, "label");
  158. for (i=0; i<nl.length; i++) {
  159. if (nl[i].getAttribute("for") == n.id || nl[i].htmlFor == n.id)
  160. this.addClass(nl[i], ic);
  161. }
  162. return null;
  163. }
  164. };