1 |
8 |
ahitrov@rambler.ru |
/** |
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 nl, i; |
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=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; |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
return true; |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
return new RegExp('^\\s*$').test(s.nodeType == 1 ? s.value : s); |
60 |
|
|
}, |
61 |
|
|
|
62 |
|
|
isNumber : function(s, d) { |
63 |
|
|
return !isNaN(s.nodeType == 1 ? s.value : s) && (!d || !this.test(s, '^-?[0-9]*\\.[0-9]*$')); |
64 |
|
|
}, |
65 |
|
|
|
66 |
|
|
test : function(s, p) { |
67 |
|
|
s = s.nodeType == 1 ? s.value : s; |
68 |
|
|
|
69 |
|
|
return s == '' || 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 i, nl, s = this.settings, c = 0; |
96 |
|
|
|
97 |
|
|
nl = this.tags(f, 'label'); |
98 |
|
|
for (i=0; i<nl.length; i++) |
99 |
|
|
this.removeClass(nl[i], s.invalid_cls); |
100 |
|
|
|
101 |
|
|
c += this.validateElms(f, 'input'); |
102 |
|
|
c += this.validateElms(f, 'select'); |
103 |
|
|
c += this.validateElms(f, 'textarea'); |
104 |
|
|
|
105 |
|
|
return c == 3; |
106 |
|
|
}, |
107 |
|
|
|
108 |
|
|
invalidate : function(n) { |
109 |
|
|
this.mark(n.form, n); |
110 |
|
|
}, |
111 |
|
|
|
112 |
|
|
reset : function(e) { |
113 |
|
|
var t = ['label', 'input', 'select', 'textarea']; |
114 |
|
|
var i, j, nl, s = this.settings; |
115 |
|
|
|
116 |
|
|
if (e == null) |
117 |
|
|
return; |
118 |
|
|
|
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); |
123 |
|
|
} |
124 |
|
|
}, |
125 |
|
|
|
126 |
|
|
validateElms : function(f, e) { |
127 |
|
|
var nl, i, n, s = this.settings, st = true, va = Validator, v; |
128 |
|
|
|
129 |
|
|
nl = this.tags(f, e); |
130 |
|
|
for (i=0; i<nl.length; i++) { |
131 |
|
|
n = nl[i]; |
132 |
|
|
|
133 |
|
|
this.removeClass(n, s.invalid_cls); |
134 |
|
|
|
135 |
|
|
if (this.hasClass(n, s.required_cls) && va.isEmpty(n)) |
136 |
|
|
st = this.mark(f, n); |
137 |
|
|
|
138 |
|
|
if (this.hasClass(n, s.number_cls) && !va.isNumber(n)) |
139 |
|
|
st = this.mark(f, n); |
140 |
|
|
|
141 |
|
|
if (this.hasClass(n, s.int_cls) && !va.isNumber(n, true)) |
142 |
|
|
st = this.mark(f, n); |
143 |
|
|
|
144 |
|
|
if (this.hasClass(n, s.url_cls) && !va.isAbsUrl(n)) |
145 |
|
|
st = this.mark(f, n); |
146 |
|
|
|
147 |
|
|
if (this.hasClass(n, s.email_cls) && !va.isEmail(n)) |
148 |
|
|
st = this.mark(f, n); |
149 |
|
|
|
150 |
|
|
if (this.hasClass(n, s.size_cls) && !va.isSize(n)) |
151 |
|
|
st = this.mark(f, n); |
152 |
|
|
|
153 |
|
|
if (this.hasClass(n, s.id_cls) && !va.isId(n)) |
154 |
|
|
st = this.mark(f, n); |
155 |
|
|
|
156 |
|
|
if (this.hasClass(n, s.min_cls, true)) { |
157 |
|
|
v = this.getNum(n, s.min_cls); |
158 |
|
|
|
159 |
|
|
if (isNaN(v) || parseInt(n.value) < parseInt(v)) |
160 |
|
|
st = this.mark(f, n); |
161 |
|
|
} |
162 |
|
|
|
163 |
|
|
if (this.hasClass(n, s.max_cls, true)) { |
164 |
|
|
v = this.getNum(n, s.max_cls); |
165 |
|
|
|
166 |
|
|
if (isNaN(v) || parseInt(n.value) > parseInt(v)) |
167 |
|
|
st = this.mark(f, n); |
168 |
|
|
} |
169 |
|
|
} |
170 |
|
|
|
171 |
|
|
return st; |
172 |
|
|
}, |
173 |
|
|
|
174 |
|
|
hasClass : function(n, c, d) { |
175 |
|
|
return new RegExp('\\b' + c + (d ? '[0-9]+' : '') + '\\b', 'g').test(n.className); |
176 |
|
|
}, |
177 |
|
|
|
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, ''); |
181 |
|
|
|
182 |
|
|
return c; |
183 |
|
|
}, |
184 |
|
|
|
185 |
|
|
addClass : function(n, c, b) { |
186 |
|
|
var o = this.removeClass(n, c); |
187 |
|
|
n.className = b ? c + (o != '' ? (' ' + o) : '') : (o != '' ? (o + ' ') : '') + c; |
188 |
|
|
}, |
189 |
|
|
|
190 |
|
|
removeClass : function(n, c) { |
191 |
|
|
c = n.className.replace(new RegExp("(^|\\s+)" + c + "(\\s+|$)"), ' '); |
192 |
|
|
return n.className = c != ' ' ? c : ''; |
193 |
|
|
}, |
194 |
|
|
|
195 |
|
|
tags : function(f, s) { |
196 |
|
|
return f.getElementsByTagName(s); |
197 |
|
|
}, |
198 |
|
|
|
199 |
|
|
mark : function(f, n) { |
200 |
|
|
var s = this.settings; |
201 |
|
|
|
202 |
|
|
this.addClass(n, s.invalid_cls); |
203 |
|
|
this.markLabels(f, n, s.invalid_cls); |
204 |
|
|
|
205 |
|
|
return false; |
206 |
|
|
}, |
207 |
|
|
|
208 |
|
|
markLabels : function(f, n, ic) { |
209 |
|
|
var nl, i; |
210 |
|
|
|
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); |
215 |
|
|
} |
216 |
|
|
|
217 |
|
|
return null; |
218 |
|
|
} |
219 |
|
|
}; |