Make the maintenance page a hardcoded response
[Melange.git] / app / tiny_mce / utils / validate.js
blobcde4c979851f50539fc9d8fea3198cce47b72cec
1 /**
2 * $Id: validate.js 758 2008-03-30 13:53:29Z spocke $
4 * Various form validation methods.
6 * @author Moxiecode
7 * @copyright Copyright © 2004-2008, Moxiecode Systems AB, All rights reserved.
8 */
10 /**
11 // String validation:
13 if (!Validator.isEmail('myemail'))
14 alert('Invalid email.');
16 // Form validation:
18 var f = document.forms['myform'];
20 if (!Validator.isEmail(f.myemail))
21 alert('Invalid email.');
24 var Validator = {
25 isEmail : function(s) {
26 return this.test(s, '^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+@[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$');
29 isAbsUrl : function(s) {
30 return this.test(s, '^(news|telnet|nttp|file|http|ftp|https)://[-A-Za-z0-9\\.]+\\/?.*$');
33 isSize : function(s) {
34 return this.test(s, '^[0-9]+(%|in|cm|mm|em|ex|pt|pc|px)?$');
37 isId : function(s) {
38 return this.test(s, '^[A-Za-z_]([A-Za-z0-9_])*$');
41 isEmpty : function(s) {
42 var nl, i;
44 if (s.nodeName == 'SELECT' && s.selectedIndex < 1)
45 return true;
47 if (s.type == 'checkbox' && !s.checked)
48 return true;
50 if (s.type == 'radio') {
51 for (i=0, nl = s.form.elements; i<nl.length; i++) {
52 if (nl[i].type == "radio" && nl[i].name == s.name && nl[i].checked)
53 return false;
56 return true;
59 return new RegExp('^\\s*$').test(s.nodeType == 1 ? s.value : s);
62 isNumber : function(s, d) {
63 return !isNaN(s.nodeType == 1 ? s.value : s) && (!d || !this.test(s, '^-?[0-9]*\\.[0-9]*$'));
66 test : function(s, p) {
67 s = s.nodeType == 1 ? s.value : s;
69 return s == '' || new RegExp(p).test(s);
73 var AutoValidator = {
74 settings : {
75 id_cls : 'id',
76 int_cls : 'int',
77 url_cls : 'url',
78 number_cls : 'number',
79 email_cls : 'email',
80 size_cls : 'size',
81 required_cls : 'required',
82 invalid_cls : 'invalid',
83 min_cls : 'min',
84 max_cls : 'max'
87 init : function(s) {
88 var n;
90 for (n in s)
91 this.settings[n] = s[n];
94 validate : function(f) {
95 var i, nl, s = this.settings, c = 0;
97 nl = this.tags(f, 'label');
98 for (i=0; i<nl.length; i++)
99 this.removeClass(nl[i], s.invalid_cls);
101 c += this.validateElms(f, 'input');
102 c += this.validateElms(f, 'select');
103 c += this.validateElms(f, 'textarea');
105 return c == 3;
108 invalidate : function(n) {
109 this.mark(n.form, n);
112 reset : function(e) {
113 var t = ['label', 'input', 'select', 'textarea'];
114 var i, j, nl, s = this.settings;
116 if (e == null)
117 return;
119 for (i=0; i<t.length; i++) {
120 nl = this.tags(e.form ? e.form : e, t[i]);
121 for (j=0; j<nl.length; j++)
122 this.removeClass(nl[j], s.invalid_cls);
126 validateElms : function(f, e) {
127 var nl, i, n, s = this.settings, st = true, va = Validator, v;
129 nl = this.tags(f, e);
130 for (i=0; i<nl.length; i++) {
131 n = nl[i];
133 this.removeClass(n, s.invalid_cls);
135 if (this.hasClass(n, s.required_cls) && va.isEmpty(n))
136 st = this.mark(f, n);
138 if (this.hasClass(n, s.number_cls) && !va.isNumber(n))
139 st = this.mark(f, n);
141 if (this.hasClass(n, s.int_cls) && !va.isNumber(n, true))
142 st = this.mark(f, n);
144 if (this.hasClass(n, s.url_cls) && !va.isAbsUrl(n))
145 st = this.mark(f, n);
147 if (this.hasClass(n, s.email_cls) && !va.isEmail(n))
148 st = this.mark(f, n);
150 if (this.hasClass(n, s.size_cls) && !va.isSize(n))
151 st = this.mark(f, n);
153 if (this.hasClass(n, s.id_cls) && !va.isId(n))
154 st = this.mark(f, n);
156 if (this.hasClass(n, s.min_cls, true)) {
157 v = this.getNum(n, s.min_cls);
159 if (isNaN(v) || parseInt(n.value) < parseInt(v))
160 st = this.mark(f, n);
163 if (this.hasClass(n, s.max_cls, true)) {
164 v = this.getNum(n, s.max_cls);
166 if (isNaN(v) || parseInt(n.value) > parseInt(v))
167 st = this.mark(f, n);
171 return st;
174 hasClass : function(n, c, d) {
175 return new RegExp('\\b' + c + (d ? '[0-9]+' : '') + '\\b', 'g').test(n.className);
178 getNum : function(n, c) {
179 c = n.className.match(new RegExp('\\b' + c + '([0-9]+)\\b', 'g'))[0];
180 c = c.replace(/[^0-9]/g, '');
182 return c;
185 addClass : function(n, c, b) {
186 var o = this.removeClass(n, c);
187 n.className = b ? c + (o != '' ? (' ' + o) : '') : (o != '' ? (o + ' ') : '') + c;
190 removeClass : function(n, c) {
191 c = n.className.replace(new RegExp("(^|\\s+)" + c + "(\\s+|$)"), ' ');
192 return n.className = c != ' ' ? c : '';
195 tags : function(f, s) {
196 return f.getElementsByTagName(s);
199 mark : function(f, n) {
200 var s = this.settings;
202 this.addClass(n, s.invalid_cls);
203 this.markLabels(f, n, s.invalid_cls);
205 return false;
208 markLabels : function(f, n, ic) {
209 var nl, i;
211 nl = this.tags(f, "label");
212 for (i=0; i<nl.length; i++) {
213 if (nl[i].getAttribute("for") == n.id || nl[i].htmlFor == n.id)
214 this.addClass(nl[i], ic);
217 return null;