forked from culturebase/cbui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.js
144 lines (127 loc) · 4.02 KB
/
validate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* This file is part of cbui.
* Copyright © 2010-2012 stiftung kulturserver.de ggmbh <[email protected]>
*
* cbui is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* cbui is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with cbui. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* validate input widgets according to classes defined on input fields (or assigned manually)
*/
/**
* Validator base class. Handles life cycle
*/
jQuery.CbValidate.validator = base2.Base.extend({
constructor : function(widget) {
this.base();
widget.validators.push(this);
},
valid : function(widget) {
return true;
},
destroy : function() {}
});
/**
* makes sure a field is neither empty nor contains the predefined string.
*/
jQuery.CbValidate.nonempty = jQuery.CbValidate.validator.extend({
valid : function(widget) {
return widget.value() != "" &&
widget.value() != jQuery.CbWidgetRegistry.bricks[widget.texts.text];
}
});
/**
* makes sure a field contains an email address.
*/
jQuery.CbValidate.email = jQuery.CbValidate.nonempty.extend({
valid : function(widget) {
return this.base(widget) &&
widget.value().match(/^[\w_\-\.]{1,}@[\w_\-\.]{2,}\.[\w]{2,4}$/);
}
});
/**
* makes sure a field contains a valid account name.
*/
jQuery.CbValidate.account = jQuery.CbValidate.email.extend({
valid : function(widget) {
return widget.value().match(/^[a-z0-9][a-z0-9\-]{0,23}[a-z0-9]$/) || this.base(widget);
}
});
/**
* makes sure a field has the same content as other fields in the same group
*/
jQuery.CbValidate.equals = jQuery.CbValidate.nonempty.extend({
/**
* constructor
* @param widget the Widget to attach to
* @param group the group this validator belongs to. If undefined 0 is assumed
*/
constructor : function(widget, group) {
var static_self = jQuery.CbValidate.equals;
if (group === undefined) group = 0;
this.base(widget);
this.widget = widget;
if (static_self.groups[group] === undefined) {
static_self.groups[group] = [];
}
this.group = static_self.groups[group];
this.group.push(this);
},
valid : function(widget) {
if (!this.base(widget)) return false;
var ret = true;
jQuery.each(this.group, function(i, group_widget) {
if (group_widget.widget.value() !== widget.value()) {
ret = false;
return false;
} else {
return true;
}
});
return ret;
},
destroy : function() {
for (index in this.group) {
if (this.group[index] == this) {
this.group.splice(index, 1);
}
}
this.base();
}
}, {
groups : {}
});
/**
* make sure the field contains a number
*/
jQuery.CbValidate.number = jQuery.CbValidate.nonempty.extend({
valid : function(widget) {
return this.base(widget) && widget.value().match(/\d*/);
}
});
jQuery.CbValidate.editingFinished = jQuery.CbValidate.validator.extend({
valid : function(widget) {return widget.editingFinished();}
});
jQuery.CbValidate.inputLength = jQuery.CbValidate.validator.extend({
constructor : function(widget, min, max) {
this.base(widget);
this.min = (typeof(min) == 'undefined') ? jQuery.CbValidate.inputLength.default_min : min;
this.max = (typeof(max) == 'undefined') ? jQuery.CbValidate.inputLength.default_max : max;
},
valid : function(widget) {
var length = widget.value().length;
return this.base(widget) && length >= this.min && length <= this.max;
}
}, {
default_min : 5,
default_max : 255
});