No regular expressions were active.
1 |
|
/**
|
|
1 |
|
/**
|
2 |
|
*
$Id:
validate.j
s
1
6
2
200
7
-0
1
-0
3
1
6
:
1
6:
5
2
Z
spocke
$
|
|
2 |
|
*
$Id:
validate.j
s
6
73
200
8
-0
3
-0
6
1
3:2
6:
2
0
Z
spocke
$
|
3 |
|
*
|
|
3 |
|
*
|
4 |
|
* Various
form vali
dation met
hods.
|
|
4 |
|
* Various
form vali
dation met
hods.
|
5 |
|
*
|
|
5 |
|
*
|
6 |
|
* @author
Moxiecode
|
|
6 |
|
* @author
Moxiecode
|
7 |
|
*
@copyright
Copyright
©
2004-200
7
,
Moxiecode
Systems
AB,
All
rights
reserved.
|
|
7 |
|
*
@copyright
Copyright
©
2004-200
8
,
Moxiecode
Systems
AB,
All
rights
reserved.
|
8 |
|
*/
|
|
8 |
|
*/
|
9 |
|
|
|
9 |
|
|
10 |
|
/**
|
|
10 |
|
/**
|
11 |
|
// Str
ing valida
tion:
|
|
11 |
|
// Str
ing valida
tion:
|
12 |
|
|
|
12 |
|
|
13 |
|
if (!V
alidator.i
sEmail('my
email'))
|
|
13 |
|
if (!V
alidator.i
sEmail('my
email'))
|
14 |
|
al
ert('Inval
id email.'
);
|
|
14 |
|
al
ert('Inval
id email.'
);
|
15 |
|
|
|
15 |
|
|
16 |
|
// For
m validati
on:
|
|
16 |
|
// For
m validati
on:
|
17 |
|
|
|
17 |
|
|
18 |
|
var f
= document
.forms['my
form'];
|
|
18 |
|
var f
= document
.forms['my
form'];
|
19 |
|
|
|
19 |
|
|
20 |
|
if (!V
alidator.i
sEmail(f.m
yemail))
|
|
20 |
|
if (!V
alidator.i
sEmail(f.m
yemail))
|
21 |
|
al
ert('Inval
id email.'
);
|
|
21 |
|
al
ert('Inval
id email.'
);
|
22 |
|
*/
|
|
22 |
|
*/
|
23 |
|
|
|
23 |
|
|
24 |
|
var Valida
tor = {
|
|
24 |
|
var Valida
tor = {
|
25 |
|
isEmai
l : functi
on(s) {
|
|
25 |
|
isEmai
l : functi
on(s) {
|
26 |
|
re
turn this.
test(s, '^
[-!#$%&\'*
+\\./0-9=?
A-Z^_`a-z{
|}~]+@[-!#
$%&\'*+\\/
0-9=?A-Z^_
`a-z{|}~]+
\.[-!#$%&\
'*+\\./0-9
=?A-Z^_`a-
z{|}~]+$')
;
|
|
26 |
|
re
turn this.
test(s, '^
[-!#$%&\'*
+\\./0-9=?
A-Z^_`a-z{
|}~]+@[-!#
$%&\'*+\\/
0-9=?A-Z^_
`a-z{|}~]+
\.[-!#$%&\
'*+\\./0-9
=?A-Z^_`a-
z{|}~]+$')
;
|
27 |
|
},
|
|
27 |
|
},
|
28 |
|
|
|
28 |
|
|
29 |
|
isAbsU
rl : funct
ion(s) {
|
|
29 |
|
isAbsU
rl : funct
ion(s) {
|
30 |
|
re
turn this.
test(s, '^
(news|teln
et|nttp|fi
le|http|ft
p|https):/
/[-A-Za-z0
-9\\.]+\\/
?.*$');
|
|
30 |
|
re
turn this.
test(s, '^
(news|teln
et|nttp|fi
le|http|ft
p|https):/
/[-A-Za-z0
-9\\.]+\\/
?.*$');
|
31 |
|
},
|
|
31 |
|
},
|
32 |
|
|
|
32 |
|
|
33 |
|
isSize
: functio
n(s) {
|
|
33 |
|
isSize
: functio
n(s) {
|
34 |
|
return
this.test(
s,
'^[0-9]+(
p
x|
%
)?$');
|
|
34 |
|
return
this.test(
s,
'^[0-9]+(
%|in|cm|mm
|em|e
x|
pt|pc|px
)?$');
|
35 |
|
},
|
|
35 |
|
},
|
36 |
|
|
|
36 |
|
|
37 |
|
isId :
function(
s) {
|
|
37 |
|
isId :
function(
s) {
|
38 |
|
re
turn this.
test(s, '^
[A-Za-z_](
[A-Za-z0-9
_])*$');
|
|
38 |
|
re
turn this.
test(s, '^
[A-Za-z_](
[A-Za-z0-9
_])*$');
|
39 |
|
},
|
|
39 |
|
},
|
40 |
|
|
|
40 |
|
|
41 |
|
isEmpt
y : functi
on(s) {
|
|
41 |
|
isEmpt
y : functi
on(s) {
|
42 |
|
va
r nl, i;
|
|
42 |
|
va
r nl, i;
|
43 |
|
|
|
43 |
|
|
44 |
|
if
(s.nodeNa
me == 'SEL
ECT' && s.
selectedIn
dex < 1)
|
|
44 |
|
if
(s.nodeNa
me == 'SEL
ECT' && s.
selectedIn
dex < 1)
|
45 |
|
return t
rue;
|
|
45 |
|
return t
rue;
|
46 |
|
|
|
46 |
|
|
47 |
|
if
(s.type =
= 'checkbo
x' && !s.c
hecked)
|
|
47 |
|
if
(s.type =
= 'checkbo
x' && !s.c
hecked)
|
48 |
|
return t
rue;
|
|
48 |
|
return t
rue;
|
49 |
|
|
|
49 |
|
|
50 |
|
if
(s.type =
= 'radio')
{
|
|
50 |
|
if
(s.type =
= 'radio')
{
|
51 |
|
for (i=0
, nl = s.f
orm.elemen
ts; i<nl.l
ength; i++
) {
|
|
51 |
|
for (i=0
, nl = s.f
orm.elemen
ts; i<nl.l
ength; i++
) {
|
52 |
|
if (
nl[i].type
== "radio
" && nl[i]
.name == s
.name && n
l[i].check
ed)
|
|
52 |
|
if (
nl[i].type
== "radio
" && nl[i]
.name == s
.name && n
l[i].check
ed)
|
53 |
|
return fal
se;
|
|
53 |
|
return fal
se;
|
54 |
|
}
|
|
54 |
|
}
|
55 |
|
|
|
55 |
|
|
56 |
|
return t
rue;
|
|
56 |
|
return t
rue;
|
57 |
|
}
|
|
57 |
|
}
|
58 |
|
|
|
58 |
|
|
59 |
|
re
turn new R
egExp('^\\
s*$').test
(s.nodeTyp
e == 1 ? s
.value : s
);
|
|
59 |
|
re
turn new R
egExp('^\\
s*$').test
(s.nodeTyp
e == 1 ? s
.value : s
);
|
60 |
|
},
|
|
60 |
|
},
|
61 |
|
|
|
61 |
|
|
62 |
|
isNumb
er : funct
ion(s, d)
{
|
|
62 |
|
isNumb
er : funct
ion(s, d)
{
|
63 |
|
re
turn !isNa
N(s.nodeTy
pe == 1 ?
s.value :
s) && (!d
|| !this.t
est(s, '^-
?[0-9]*\\.
[0-9]*$'))
;
|
|
63 |
|
re
turn !isNa
N(s.nodeTy
pe == 1 ?
s.value :
s) && (!d
|| !this.t
est(s, '^-
?[0-9]*\\.
[0-9]*$'))
;
|
64 |
|
},
|
|
64 |
|
},
|
65 |
|
|
|
65 |
|
|
66 |
|
test :
function(
s, p) {
|
|
66 |
|
test :
function(
s, p) {
|
67 |
|
s
= s.nodeTy
pe == 1 ?
s.value :
s;
|
|
67 |
|
s
= s.nodeTy
pe == 1 ?
s.value :
s;
|
68 |
|
|
|
68 |
|
|
69 |
|
re
turn s ==
'' || new
RegExp(p).
test(s);
|
|
69 |
|
re
turn s ==
'' || new
RegExp(p).
test(s);
|
70 |
|
}
|
|
70 |
|
}
|
71 |
|
};
|
|
71 |
|
};
|
72 |
|
|
|
72 |
|
|
73 |
|
var AutoVa
lidator =
{
|
|
73 |
|
var AutoVa
lidator =
{
|
74 |
|
settin
gs : {
|
|
74 |
|
settin
gs : {
|
75 |
|
id
_cls : 'id
',
|
|
75 |
|
id
_cls : 'id
',
|
76 |
|
in
t_cls : 'i
nt',
|
|
76 |
|
in
t_cls : 'i
nt',
|
77 |
|
ur
l_cls : 'u
rl',
|
|
77 |
|
ur
l_cls : 'u
rl',
|
78 |
|
nu
mber_cls :
'number',
|
|
78 |
|
nu
mber_cls :
'number',
|
79 |
|
em
ail_cls :
'email',
|
|
79 |
|
em
ail_cls :
'email',
|
80 |
|
si
ze_cls : '
size',
|
|
80 |
|
si
ze_cls : '
size',
|
81 |
|
re
quired_cls
: 'requir
ed',
|
|
81 |
|
re
quired_cls
: 'requir
ed',
|
82 |
|
in
valid_cls
: 'invalid
',
|
|
82 |
|
in
valid_cls
: 'invalid
',
|
83 |
|
mi
n_cls : 'm
in',
|
|
83 |
|
mi
n_cls : 'm
in',
|
84 |
|
ma
x_cls : 'm
ax'
|
|
84 |
|
ma
x_cls : 'm
ax'
|
85 |
|
},
|
|
85 |
|
},
|
86 |
|
|
|
86 |
|
|
87 |
|
init :
function(
s) {
|
|
87 |
|
init :
function(
s) {
|
88 |
|
va
r n;
|
|
88 |
|
va
r n;
|
89 |
|
|
|
89 |
|
|
90 |
|
fo
r (n in s)
|
|
90 |
|
fo
r (n in s)
|
91 |
|
this.set
tings[n] =
s[n];
|
|
91 |
|
this.set
tings[n] =
s[n];
|
92 |
|
},
|
|
92 |
|
},
|
93 |
|
|
|
93 |
|
|
94 |
|
valida
te : funct
ion(f) {
|
|
94 |
|
valida
te : funct
ion(f) {
|
95 |
|
va
r i, nl, s
= this.se
ttings, c
= 0;
|
|
95 |
|
va
r i, nl, s
= this.se
ttings, c
= 0;
|
96 |
|
|
|
96 |
|
|
97 |
|
nl
= this.ta
gs(f, 'lab
el');
|
|
97 |
|
nl
= this.ta
gs(f, 'lab
el');
|
98 |
|
fo
r (i=0; i<
nl.length;
i++)
|
|
98 |
|
fo
r (i=0; i<
nl.length;
i++)
|
99 |
|
this.rem
oveClass(n
l[i], s.in
valid_cls)
;
|
|
99 |
|
this.rem
oveClass(n
l[i], s.in
valid_cls)
;
|
100 |
|
|
|
100 |
|
|
101 |
|
c
+= this.va
lidateElms
(f, 'input
');
|
|
101 |
|
c
+= this.va
lidateElms
(f, 'input
');
|
102 |
|
c
+= this.va
lidateElms
(f, 'selec
t');
|
|
102 |
|
c
+= this.va
lidateElms
(f, 'selec
t');
|
103 |
|
c
+= this.va
lidateElms
(f, 'texta
rea');
|
|
103 |
|
c
+= this.va
lidateElms
(f, 'texta
rea');
|
104 |
|
|
|
104 |
|
|
105 |
|
re
turn c ==
3;
|
|
105 |
|
re
turn c ==
3;
|
106 |
|
},
|
|
106 |
|
},
|
107 |
|
|
|
107 |
|
|
108 |
|
invali
date : fun
ction(n) {
|
|
108 |
|
invali
date : fun
ction(n) {
|
109 |
|
th
is.mark(n.
form, n);
|
|
109 |
|
th
is.mark(n.
form, n);
|
110 |
|
},
|
|
110 |
|
},
|
111 |
|
|
|
111 |
|
|
112 |
|
reset
: function
(e) {
|
|
112 |
|
reset
: function
(e) {
|
113 |
|
va
r t = new
Array('lab
el', 'inpu
t', 'selec
t', 'texta
rea');
|
|
113 |
|
va
r t = new
Array('lab
el', 'inpu
t', 'selec
t', 'texta
rea');
|
114 |
|
va
r i, j, nl
, s = this
.settings;
|
|
114 |
|
va
r i, j, nl
, s = this
.settings;
|
115 |
|
|
|
115 |
|
|
116 |
|
if
(e == nul
l)
|
|
116 |
|
if
(e == nul
l)
|
117 |
|
return;
|
|
117 |
|
return;
|
118 |
|
|
|
118 |
|
|
119 |
|
fo
r (i=0; i<
t.length;
i++) {
|
|
119 |
|
fo
r (i=0; i<
t.length;
i++) {
|
120 |
|
nl = thi
s.tags(e.f
orm ? e.fo
rm : e, t[
i]);
|
|
120 |
|
nl = thi
s.tags(e.f
orm ? e.fo
rm : e, t[
i]);
|
121 |
|
for (j=0
; j<nl.len
gth; j++)
|
|
121 |
|
for (j=0
; j<nl.len
gth; j++)
|
122 |
|
this
.removeCla
ss(nl[j],
s.invalid_
cls);
|
|
122 |
|
this
.removeCla
ss(nl[j],
s.invalid_
cls);
|
123 |
|
}
|
|
123 |
|
}
|
124 |
|
},
|
|
124 |
|
},
|
125 |
|
|
|
125 |
|
|
126 |
|
valida
teElms : f
unction(f,
e) {
|
|
126 |
|
valida
teElms : f
unction(f,
e) {
|
127 |
|
va
r nl, i, n
, s = this
.settings,
st = true
, va = Val
idator, v;
|
|
127 |
|
va
r nl, i, n
, s = this
.settings,
st = true
, va = Val
idator, v;
|
128 |
|
|
|
128 |
|
|
129 |
|
nl
= this.ta
gs(f, e);
|
|
129 |
|
nl
= this.ta
gs(f, e);
|
130 |
|
fo
r (i=0; i<
nl.length;
i++) {
|
|
130 |
|
fo
r (i=0; i<
nl.length;
i++) {
|
131 |
|
n = nl[i
];
|
|
131 |
|
n = nl[i
];
|
132 |
|
|
|
132 |
|
|
133 |
|
this.rem
oveClass(n
, s.invali
d_cls);
|
|
133 |
|
this.rem
oveClass(n
, s.invali
d_cls);
|
134 |
|
|
|
134 |
|
|
135 |
|
if (this
.hasClass(
n, s.requi
red_cls) &
& va.isEmp
ty(n))
|
|
135 |
|
if (this
.hasClass(
n, s.requi
red_cls) &
& va.isEmp
ty(n))
|
136 |
|
st =
this.mark
(f, n);
|
|
136 |
|
st =
this.mark
(f, n);
|
137 |
|
|
|
137 |
|
|
138 |
|
if (this
.hasClass(
n, s.numbe
r_cls) &&
!va.isNumb
er(n))
|
|
138 |
|
if (this
.hasClass(
n, s.numbe
r_cls) &&
!va.isNumb
er(n))
|
139 |
|
st =
this.mark
(f, n);
|
|
139 |
|
st =
this.mark
(f, n);
|
140 |
|
|
|
140 |
|
|
141 |
|
if (this
.hasClass(
n, s.int_c
ls) && !va
.isNumber(
n, true))
|
|
141 |
|
if (this
.hasClass(
n, s.int_c
ls) && !va
.isNumber(
n, true))
|
142 |
|
st =
this.mark
(f, n);
|
|
142 |
|
st =
this.mark
(f, n);
|
143 |
|
|
|
143 |
|
|
144 |
|
if (this
.hasClass(
n, s.url_c
ls) && !va
.isAbsUrl(
n))
|
|
144 |
|
if (this
.hasClass(
n, s.url_c
ls) && !va
.isAbsUrl(
n))
|
145 |
|
st =
this.mark
(f, n);
|
|
145 |
|
st =
this.mark
(f, n);
|
146 |
|
|
|
146 |
|
|
147 |
|
if (this
.hasClass(
n, s.email
_cls) && !
va.isEmail
(n))
|
|
147 |
|
if (this
.hasClass(
n, s.email
_cls) && !
va.isEmail
(n))
|
148 |
|
st =
this.mark
(f, n);
|
|
148 |
|
st =
this.mark
(f, n);
|
149 |
|
|
|
149 |
|
|
150 |
|
if (this
.hasClass(
n, s.size_
cls) && !v
a.isSize(n
))
|
|
150 |
|
if (this
.hasClass(
n, s.size_
cls) && !v
a.isSize(n
))
|
151 |
|
st =
this.mark
(f, n);
|
|
151 |
|
st =
this.mark
(f, n);
|
152 |
|
|
|
152 |
|
|
153 |
|
if (this
.hasClass(
n, s.id_cl
s) && !va.
isId(n))
|
|
153 |
|
if (this
.hasClass(
n, s.id_cl
s) && !va.
isId(n))
|
154 |
|
st =
this.mark
(f, n);
|
|
154 |
|
st =
this.mark
(f, n);
|
155 |
|
|
|
155 |
|
|
156 |
|
if (this
.hasClass(
n, s.min_c
ls, true))
{
|
|
156 |
|
if (this
.hasClass(
n, s.min_c
ls, true))
{
|
157 |
|
v =
this.getNu
m(n, s.min
_cls);
|
|
157 |
|
v =
this.getNu
m(n, s.min
_cls);
|
158 |
|
|
|
158 |
|
|
159 |
|
if (
isNaN(v) |
| parseInt
(n.value)
< parseInt
(v))
|
|
159 |
|
if (
isNaN(v) |
| parseInt
(n.value)
< parseInt
(v))
|
160 |
|
st = this.
mark(f, n)
;
|
|
160 |
|
st = this.
mark(f, n)
;
|
161 |
|
}
|
|
161 |
|
}
|
162 |
|
|
|
162 |
|
|
163 |
|
if (this
.hasClass(
n, s.max_c
ls, true))
{
|
|
163 |
|
if (this
.hasClass(
n, s.max_c
ls, true))
{
|
164 |
|
v =
this.getNu
m(n, s.max
_cls);
|
|
164 |
|
v =
this.getNu
m(n, s.max
_cls);
|
165 |
|
|
|
165 |
|
|
166 |
|
if (
isNaN(v) |
| parseInt
(n.value)
> parseInt
(v))
|
|
166 |
|
if (
isNaN(v) |
| parseInt
(n.value)
> parseInt
(v))
|
167 |
|
st = this.
mark(f, n)
;
|
|
167 |
|
st = this.
mark(f, n)
;
|
168 |
|
}
|
|
168 |
|
}
|
169 |
|
}
|
|
169 |
|
}
|
170 |
|
|
|
170 |
|
|
171 |
|
re
turn st;
|
|
171 |
|
re
turn st;
|
172 |
|
},
|
|
172 |
|
},
|
173 |
|
|
|
173 |
|
|
174 |
|
hasCla
ss : funct
ion(n, c,
d) {
|
|
174 |
|
hasCla
ss : funct
ion(n, c,
d) {
|
175 |
|
re
turn new R
egExp('\\b
' + c + (d
? '[0-9]+
' : '') +
'\\b', 'g'
).test(n.c
lassName);
|
|
175 |
|
re
turn new R
egExp('\\b
' + c + (d
? '[0-9]+
' : '') +
'\\b', 'g'
).test(n.c
lassName);
|
176 |
|
},
|
|
176 |
|
},
|
177 |
|
|
|
177 |
|
|
178 |
|
getNum
: functio
n(n, c) {
|
|
178 |
|
getNum
: functio
n(n, c) {
|
179 |
|
c
= n.classN
ame.match(
new RegExp
('\\b' + c
+ '([0-9]
+)\\b', 'g
'))[0];
|
|
179 |
|
c
= n.classN
ame.match(
new RegExp
('\\b' + c
+ '([0-9]
+)\\b', 'g
'))[0];
|
180 |
|
c
= c.replac
e(/[^0-9]/
g, '');
|
|
180 |
|
c
= c.replac
e(/[^0-9]/
g, '');
|
181 |
|
|
|
181 |
|
|
182 |
|
re
turn c;
|
|
182 |
|
re
turn c;
|
183 |
|
},
|
|
183 |
|
},
|
184 |
|
|
|
184 |
|
|
185 |
|
addCla
ss : funct
ion(n, c,
b) {
|
|
185 |
|
addCla
ss : funct
ion(n, c,
b) {
|
186 |
|
va
r o = this
.removeCla
ss(n, c);
|
|
186 |
|
va
r o = this
.removeCla
ss(n, c);
|
187 |
|
n.
className
= b ? c +
(o != '' ?
(' ' + o)
: '') : (
o != '' ?
(o + ' ')
: '') + c;
|
|
187 |
|
n.
className
= b ? c +
(o != '' ?
(' ' + o)
: '') : (
o != '' ?
(o + ' ')
: '') + c;
|
188 |
|
},
|
|
188 |
|
},
|
189 |
|
|
|
189 |
|
|
190 |
|
remove
Class : fu
nction(n,
c) {
|
|
190 |
|
remove
Class : fu
nction(n,
c) {
|
191 |
|
c
= n.classN
ame.replac
e(new RegE
xp("(^|\\s
+)" + c +
"(\\s+|$)"
), ' ');
|
|
191 |
|
c
= n.classN
ame.replac
e(new RegE
xp("(^|\\s
+)" + c +
"(\\s+|$)"
), ' ');
|
192 |
|
re
turn n.cla
ssName = c
!= ' ' ?
c : '';
|
|
192 |
|
re
turn n.cla
ssName = c
!= ' ' ?
c : '';
|
193 |
|
},
|
|
193 |
|
},
|
194 |
|
|
|
194 |
|
|
195 |
|
tags :
function(
f, s) {
|
|
195 |
|
tags :
function(
f, s) {
|
196 |
|
re
turn f.get
ElementsBy
TagName(s)
;
|
|
196 |
|
re
turn f.get
ElementsBy
TagName(s)
;
|
197 |
|
},
|
|
197 |
|
},
|
198 |
|
|
|
198 |
|
|
199 |
|
mark :
function(
f, n) {
|
|
199 |
|
mark :
function(
f, n) {
|
200 |
|
va
r s = this
.settings;
|
|
200 |
|
va
r s = this
.settings;
|
201 |
|
|
|
201 |
|
|
202 |
|
th
is.addClas
s(n, s.inv
alid_cls);
|
|
202 |
|
th
is.addClas
s(n, s.inv
alid_cls);
|
203 |
|
th
is.markLab
els(f, n,
s.invalid_
cls);
|
|
203 |
|
th
is.markLab
els(f, n,
s.invalid_
cls);
|
204 |
|
|
|
204 |
|
|
205 |
|
re
turn false
;
|
|
205 |
|
re
turn false
;
|
206 |
|
},
|
|
206 |
|
},
|
207 |
|
|
|
207 |
|
|
208 |
|
markLa
bels : fun
ction(f, n
, ic) {
|
|
208 |
|
markLa
bels : fun
ction(f, n
, ic) {
|
209 |
|
va
r nl, i;
|
|
209 |
|
va
r nl, i;
|
210 |
|
|
|
210 |
|
|
211 |
|
nl
= this.ta
gs(f, "lab
el");
|
|
211 |
|
nl
= this.ta
gs(f, "lab
el");
|
212 |
|
fo
r (i=0; i<
nl.length;
i++) {
|
|
212 |
|
fo
r (i=0; i<
nl.length;
i++) {
|
213 |
|
if (nl[i
].getAttri
bute("for"
) == n.id
|| nl[i].h
tmlFor ==
n.id)
|
|
213 |
|
if (nl[i
].getAttri
bute("for"
) == n.id
|| nl[i].h
tmlFor ==
n.id)
|
214 |
|
this
.addClass(
nl[i], ic)
;
|
|
214 |
|
this
.addClass(
nl[i], ic)
;
|
215 |
|
}
|
|
215 |
|
}
|
216 |
|
|
|
216 |
|
|
217 |
|
re
turn null;
|
|
217 |
|
re
turn null;
|
218 |
|
}
|
|
218 |
|
}
|
219 |
|
};
|
|
219 |
|
};
|