-
Notifications
You must be signed in to change notification settings - Fork 138
/
developing.html
executable file
·296 lines (250 loc) · 10.9 KB
/
developing.html
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
---
layout: default
title: Developing
permalink: /developing/
---
<div class="doc-content-container">
<div class="doc-heading">
<h1>Developing</h1>
<p>It's quite easy to develop new validator</p>
{% include edit-button.html %}
{% include share.html %}
</div>
<div class="container">
<div class="row">
<div class="col-md-9">
<section>
<h2>Writing new validator</h2>
<p>A validator has to follow the syntax:</p>
{% highlight javascript %}
(function($) {
$.fn.bootstrapValidator.validators.validatorName = {
/**
* @param {BootstrapValidator} validator The validator plugin instance
* @param {jQuery} $field The jQuery object represents the field element
* @param {Object} options The validator options
* @returns {Boolean}
*/
validate: function(validator, $field, options) {
// You can get the field value
// var value = $field.val();
//
// Perform validating
// ...
//
// return true if the field value is valid
// otherwise return false
}
};
}(window.jQuery));
{% endhighlight %}
<p>The validator must implement <code>validate()</code> method that returns <code>true</code> if the field is valid, or <code>false</code> otherwise.</p>
<p><code>validatorName</code> is the validator name. Since the validators are distinct by the names, <code>validatorName</code> has to be different with <a href="/validators/">built-in validators</a>.</p>
<p>To apply the validator to particular field:</p>
{% highlight javascript %}
$(form).bootstrapValidator({
fields: {
fieldName: {
// Replace validatorName with the name of validator
// validatorOptions will be passed as third parameter of the
// validate(validator, $field, options) method
validatorName: validatorOptions
}
}
});{% endhighlight %}
<div class="doc-alert doc-alert-info">
To see how built-in validators are developed, you can look at their sources located at the <a href="{{ site.repository.source }}/tree/master/src/js/validator">src/js/validator</a> directory.
</div>
<h3>Dynamic message</h3>
<p>If you want to set dynamic error message, the validator must return an object that consists of <code>valid</code> and <code>message</code> members:</p>
{% highlight javascript %}
(function($) {
$.fn.bootstrapValidator.validators.validatorName = {
validate: function(validator, $field, options) {
// ... Do your logic checking
if (...) {
return {
valid: true, // or false
message: 'The error message'
}
}
return {
valid: false, // or true
message: 'Other error message'
}
}
};
}(window.jQuery));
{% endhighlight %}
</section>
<div class="doc-hr"></div>
<section>
<h2>Example</h2>
<p>The following example illustrates how to develop a simple validator which validate a password. The validator will treat a password as valid, if it satisfies all the conditions below:</p>
<ul class="doc-list">
<li>Must be more than 8 characters long</li>
<li>Must contain at least one upper case character</li>
<li>Must contain at least one lower case character</li>
<li>Must contain at least one digit</li>
</ul>
<p>In fact, you can add more conditions to make a secure password.</p>
{% highlight javascript %}
(function($) {
$.fn.bootstrapValidator.validators.password = {
validate: function(validator, $field, options) {
var value = $field.val();
if (value === '') {
return true;
}
// Check the password strength
if (value.length < 8) {
return false;
}
// The password doesn't contain any uppercase character
if (value === value.toLowerCase()) {
return false;
}
// The password doesn't contain any uppercase character
if (value === value.toUpperCase()) {
return false;
}
// The password doesn't contain any digit
if (value.search(/[0-9]/) < 0) {
return false;
}
return true;
}
};
}(window.jQuery));
{% endhighlight %}
<div class="doc-demo">
<ul class="nav nav-tabs">
<li class="active"><a href="#password-form-tab" data-toggle="tab">Try it</a></li>
<li><a href="#password-html-tab" data-toggle="tab">HTML</a></li>
<li><a href="#password-js-tab" data-toggle="tab">JS</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="password-form-tab">
{% include developing/password-example.html %}
</div>
<div class="tab-pane" id="password-html-tab">
{% highlight html %}
{% include developing/password-example.html %}
{% endhighlight %}
</div>
<div class="tab-pane" id="password-js-tab">
{% highlight javascript %}
{% include developing/password-example.js %}
{% endhighlight %}
</div>
</div>
</div>
<p>Basically, the validator works fine. It returns <code>false</code> if the password doesn't satisfy any of conditions we define. The limitation here is that the user don't know
which condition the password doesn't pass. It informs the same <code>The password is not valid</code> message in all cases.</p>
<p>Using <a href="#dynamic-message">dynamic message</a> ability, it's easy to make the validator more friendly.</p>
{% highlight javascript %}
(function($) {
$.fn.bootstrapValidator.validators.securePassword = {
validate: function(validator, $field, options) {
var value = $field.val();
if (value === '') {
return true;
}
// Check the password strength
if (value.length < 8) {
return {
valid: false,
message: 'The password must be more than 8 characters long'
};
}
// The password doesn't contain any uppercase character
if (value === value.toLowerCase()) {
return {
valid: false,
message: 'The password must contain at least one upper case character'
}
}
// The password doesn't contain any uppercase character
if (value === value.toUpperCase()) {
return {
valid: false,
message: 'The password must contain at least one lower case character'
}
}
// The password doesn't contain any digit
if (value.search(/[0-9]/) < 0) {
return {
valid: false,
message: 'The password must contain at least one digit'
}
}
return true;
}
};
}(window.jQuery));
{% endhighlight %}
<p>Now, the form shows exactly condition that we want the password to satisfy.</p>
<div class="doc-demo">
<ul class="nav nav-tabs">
<li class="active"><a href="#secure-password-form-tab" data-toggle="tab">Try it</a></li>
<li><a href="#secure-password-html-tab" data-toggle="tab">HTML</a></li>
<li><a href="#secure-password-js-tab" data-toggle="tab">JS</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="secure-password-form-tab">
{% include developing/secure-password-example.html %}
</div>
<div class="tab-pane" id="secure-password-html-tab">
{% highlight html %}
{% include developing/secure-password-example.html %}
{% endhighlight %}
</div>
<div class="tab-pane" id="secure-password-js-tab">
{% highlight javascript %}
{% include developing/secure-password-example.js %}
{% endhighlight %}
</div>
</div>
</div>
</section>
<div class="doc-hr"></div>
<section>
<h2>Building</h2>
<p>BootstrapValidator uses <a href="http://gruntjs.com">grunt</a> to simplify building process.</p>
<p>From the BootstrapValidator root directory, install dependencies:</p>
{% highlight bash %}
$ npm install
{% endhighlight %}
<p>Then, uses grunt to build:</p>
{% highlight bash %}
$ grunt
{% endhighlight %}
<p>If you want grunt to generate scripts whenever the original scripts (located in <code>src</code>) change, use the following command:</p>
{% highlight bash %}
$ grunt watch
{% endhighlight %}
<p>The generated scripts (including source and compressed versions) are placed inside the <code>dist</code> directory.</p>
</section>
<div class="doc-hr"></div>
<section>
<h2>Contributing</h2>
<p>If you develop new validator which might be useful for other one, please contribute it to the project.</p>
<p>It can be done via 3 steps:</p>
<ul class="doc-list">
<li><a href="{{ site.repository.source }}/fork">Fork</a> the project</li>
<li>Develop new validator, suggest new feature or fix a bug</li>
<li>Pull a new request</li>
</ul>
<p>All pull requests are welcome <i class="fa fa-heart"></i>.</p>
</section>
</div>
<div class="col-md-3">
<div id="toc" class="doc-toc hidden-print hidden-xs hidden-sm"></div>
</div>
</div>
</div>
</div>
<script>
{% include developing/password-example.js %}
{% include developing/secure-password-example.js %}
</script>