Open-Source PHP Framework - Designed for rapid development of performance-oriented scalable applications

/webroot/js/tinymce/utils/validate.js

[return to app]
1 /**
2  * $Id: validate.js 758 2008-03-30 13:53:29Z spocke $
3  *
4  * Various form validation methods.
5  *
6  * @author Moxiecode
7  * @copyright Copyright © 2004-2008, Moxiecode Systems AB, All rights reserved.
8  */
9
10 /**
11     // String validation:
12
13     if (!Validator.isEmail('myemail'))
14         alert('Invalid email.');
15
16     // Form validation:
17
18     var f = document.forms['myform'];
19
20     if (!Validator.isEmail(f.myemail))
21         alert('Invalid email.');
22 */
23
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{|}~]+$');
27     },
28
29     
isAbsUrl : function(s) {
30         return 
this.test(s'^(news|telnet|nttp|file|http|ftp|https)://[-A-Za-z0-9\\.]+\\/?.*$');
31     },
32
33     
isSize : function(s) {
34         return 
this.test(s'^[0-9]+(%|in|cm|mm|em|ex|pt|pc|px)?$');
35     },
36
37     
isId : function(s) {
38         return 
this.test(s'^[A-Za-z_]([A-Za-z0-9_])*$');
39     },
40
41     
isEmpty : function(s) {
42         var 
nli;
43
44         if (
s.nodeName == 'SELECT' && s.selectedIndex 1)
45             return 
true;
46
47         if (
s.type == 'checkbox' && !s.checked)
48             return 
true;
49
50         if (
s.type == 'radio') {
51             for (
i=0nl s.form.elementsi<nl.lengthi++) {
52                 if (
nl[i].type == "radio" && nl[i].name == s.name && nl[i].checked)
53                     return 
false;
54             }
55
56             return 
true;
57         }
58
59         return new 
RegExp('^\\s*$').test(s.nodeType == s.value s);
60     },
61
62     
isNumber : function(sd) {
63         return !
isNaN(s.nodeType == s.value s) && (!|| !this.test(s'^-?[0-9]*\\.[0-9]*$'));
64     },
65
66     
test : function(sp) {
67         
s.nodeType == s.value s;
68
69         return 
== '' || new RegExp(p).test(s);
70     }
71 };
72
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'
85     
},
86
87     
init : function(s) {
88         var 
n;
89
90         for (
n in s)
91             
this.settings[n] = s[n];
92     },
93
94     
validate : function(f) {
95         var 
inlthis.settings0;
96
97         
nl this.tags(f'label');
98         for (
i=0i<nl.lengthi++)
99             
this.removeClass(nl[i], s.invalid_cls);
100
101         
+= this.validateElms(f'input');
102         
+= this.validateElms(f'select');
103         
+= this.validateElms(f'textarea');
104
105         return 
== 3;
106     },
107
108     
invalidate : function(n) {
109         
this.mark(n.formn);
110     },
111
112     
reset : function(e) {
113         var 
= ['label''input''select''textarea'];
114         var 
ijnlthis.settings;
115
116         if (
== null)
117             return;
118
119         for (
i=0i<t.lengthi++) {
120             
nl this.tags(e.form e.form et[i]);
121             for (
j=0j<nl.lengthj++)
122                 
this.removeClass(nl[j], s.invalid_cls);
123         }
124     },
125
126     
validateElms : function(fe) {
127         var 
nlinthis.settingsst trueva Validatorv;
128
129         
nl this.tags(fe);
130         for (
i=0i<nl.lengthi++) {
131             
nl[i];
132
133             
this.removeClass(ns.invalid_cls);
134
135             if (
this.hasClass(ns.required_cls) && va.isEmpty(n))
136                 
st this.mark(fn);
137
138             if (
this.hasClass(ns.number_cls) && !va.isNumber(n))
139                 
st this.mark(fn);
140
141             if (
this.hasClass(ns.int_cls) && !va.isNumber(ntrue))
142                 
st this.mark(fn);
143
144             if (
this.hasClass(ns.url_cls) && !va.isAbsUrl(n))
145                 
st this.mark(fn);
146
147             if (
this.hasClass(ns.email_cls) && !va.isEmail(n))
148                 
st this.mark(fn);
149
150             if (
this.hasClass(ns.size_cls) && !va.isSize(n))
151                 
st this.mark(fn);
152
153             if (
this.hasClass(ns.id_cls) && !va.isId(n))
154                 
st this.mark(fn);
155
156             if (
this.hasClass(ns.min_clstrue)) {
157                 
this.getNum(ns.min_cls);
158
159                 if (
isNaN(v) || parseInt(n.value) < parseInt(v))
160                     
st this.mark(fn);
161             }
162
163             if (
this.hasClass(ns.max_clstrue)) {
164                 
this.getNum(ns.max_cls);
165
166                 if (
isNaN(v) || parseInt(n.value) > parseInt(v))
167                     
st this.mark(fn);
168             }
169         }
170
171         return 
st;
172     },
173
174     
hasClass : function(ncd) {
175         return new 
RegExp('\\b' + ('[0-9]+' '') + '\\b''g').test(n.className);
176     },
177
178     
getNum : function(nc) {
179         
n.className.match(new RegExp('\\b' '([0-9]+)\\b''g'))[0];
180         
c.replace(/[^0-9]/g'');
181
182         return 
c;
183     },
184
185     
addClass : function(ncb) {
186         var 
this.removeClass(nc);
187         
n.className + (!= '' ? (' ' o) : '') : (!= '' ? (' ') : '') + c;
188     },
189
190     
removeClass : function(nc) {
191         
n.className.replace(new RegExp("(^|\\s+)" "(\\s+|$)"), ' ');
192         return 
n.className != ' ' '';
193     },
194
195     
tags : function(fs) {
196         return 
f.getElementsByTagName(s);
197     },
198
199     
mark : function(fn) {
200         var 
this.settings;
201
202         
this.addClass(ns.invalid_cls);
203         
this.markLabels(fns.invalid_cls);
204
205         return 
false;
206     },
207
208     
markLabels : function(fnic) {
209         var 
nli;
210
211         
nl this.tags(f"label");
212         for (
i=0i<nl.lengthi++) {
213             if (
nl[i].getAttribute("for") == n.id || nl[i].htmlFor == n.id)
214                 
this.addClass(nl[i], ic);
215         }
216
217         return 
null;
218     }
219 };
220