forked from 1000hz/bootstrap-validator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
482 lines (431 loc) · 32.7 KB
/
index.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Meta, title, CSS, favicons, etc. -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<title>
Validator, for Bootstrap 3
</title>
<!-- Bootstrap core CSS -->
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<!-- Documentation extras -->
<link href="assets/css/docs.css" rel="stylesheet">
<link href="assets/css/pygments-manni.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
<!-- Favicons -->
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="assets/ico/apple-touch-icon-144-precomposed.png">
<link rel="shortcut icon" href="assets/ico/favicon.png">
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-45851020-1', '1000hz.github.io');
ga('send', 'pageview');
</script>
</head>
<body>
<a class="sr-only" href="#content">Skip to main content</a>
<!-- Docs master nav -->
<header class="navbar navbar-inverse navbar-fixed-top bs-docs-nav" role="banner">
<div class="container">
<div class="navbar-header">
<button class="navbar-toggle" type="button" data-toggle="collapse" data-target=".bs-navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="#" class="navbar-brand">Bootstrap Validator</a>
</div>
<nav class="collapse navbar-collapse bs-navbar-collapse" role="navigation">
<ul class="nav navbar-nav navbar-right">
<li>
<a href="https://github.com/1000hz/bootstrap-validator">View on GitHub</a>
</li>
</ul>
</nav>
</div>
</header>
<!-- Docs page layout -->
<div class="bs-header" id="content">
<div class="container">
<h1>Validator</h1>
<p>A simple and user-friendly form validator plugin for Bootstrap 3</p>
<script async type="text/javascript" src="//cdn.carbonads.com/carbon.js?zoneid=1673&serve=C6AILKT&placement=1000hzgithubio" id="_carbonads_js"></script>
</div>
</div>
<div class="container bs-docs-container">
<div class="row">
<div class="col-md-3">
<div class="bs-sidebar hidden-print" role="complementary">
<ul class="nav bs-sidenav">
<li class="active">
<a href="#validator">Validator</a>
<ul class="nav">
<li><a href="#validator-overview">Overview</a></li>
<li><a href="#validator-examples">Examples</a></li>
<li><a href="#validator-usage">Usage</a></li>
<li><a href="#validator-markup">Markup</a></li>
<li><a href="#validator-fields">Validated fields</a></li>
<li><a href="#validator-options">Options</a></li>
<li><a href="#validator-methods">Methods</a></li>
<li><a href="#validator-events">Events</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="col-md-9" role="main">
<!-- Dropdowns
================================================== -->
<div class="bs-docs-section">
<div class="page-header">
<h1 id="validator">Validator <small>validator.js</small></h1>
</div>
<h2 id="validator-Overview">Overview</h2>
<p>This plugin adheres to the <a href="http://getbootstrap.com/javascript/#js-overview">conventions</a> set forth by Bootstrap's core jQuery plugins, so be sure to check those out to get a better understanding of the goals and design of this plugin.</p>
<h2 id="validator-examples">Examples</h2>
<p>Add validation to your forms with this simple plugin.</p>
<div class="bs-example">
<form data-toggle="validator" role="form">
<div class="form-group">
<label for="inputName" class="control-label">Name</label>
<input type="text" class="form-control" id="inputName" placeholder="Cina Saffary" required>
</div>
<div class="form-group has-feedback">
<label for="inputTwitter" class="control-label">Twitter</label>
<div class="input-group">
<span class="input-group-addon">@</span>
<input type="text" pattern="^[_A-z0-9]{1,}$" maxlength="15" class="form-control" id="inputTwitter" placeholder="1000hz" required>
</div>
<span class="glyphicon form-control-feedback" aria-hidden="true"></span>
<div class="help-block with-errors">Hey look, this one has feedback icons!</div>
</div>
<div class="form-group">
<label for="inputEmail" class="control-label">Email</label>
<input type="email" class="form-control" id="inputEmail" placeholder="Email" data-error="Bruh, that email address is invalid" required>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="inputPassword" class="control-label">Password</label>
<div class="form-inline row">
<div class="form-group col-sm-6">
<input type="password" data-minlength="6" class="form-control" id="inputPassword" placeholder="Password" required>
<div class="help-block">Minimum of 6 characters</div>
</div>
<div class="form-group col-sm-6">
<input type="password" class="form-control" id="inputPasswordConfirm" data-match="#inputPassword" data-match-error="Whoops, these don't match" placeholder="Confirm" required>
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="form-group">
<div class="radio">
<label>
<input type="radio" name="underwear" required>
Boxers
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="underwear" required>
Briefs
</label>
</div>
</div>
<div class="form-group">
<div class="checkbox">
<label>
<input type="checkbox" id="terms" data-error="Before you wreck yourself" required>
Check yourself
</label>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div> <!-- /example -->
<figure class="highlight"><pre><code class="language-html" data-lang="html"><span class="nt"><form</span> <span class="na">data-toggle=</span><span class="s">"validator"</span> <span class="na">role=</span><span class="s">"form"</span><span class="nt">></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"form-group"</span><span class="nt">></span>
<span class="nt"><label</span> <span class="na">for=</span><span class="s">"inputName"</span> <span class="na">class=</span><span class="s">"control-label"</span><span class="nt">></span>Name<span class="nt"></label></span>
<span class="nt"><input</span> <span class="na">type=</span><span class="s">"text"</span> <span class="na">class=</span><span class="s">"form-control"</span> <span class="na">id=</span><span class="s">"inputName"</span> <span class="na">placeholder=</span><span class="s">"Cina Saffary"</span> <span class="na">required</span><span class="nt">></span>
<span class="nt"></div></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"form-group has-feedback"</span><span class="nt">></span>
<span class="nt"><label</span> <span class="na">for=</span><span class="s">"inputTwitter"</span> <span class="na">class=</span><span class="s">"control-label"</span><span class="nt">></span>Twitter<span class="nt"></label></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"input-group"</span><span class="nt">></span>
<span class="nt"><span</span> <span class="na">class=</span><span class="s">"input-group-addon"</span><span class="nt">></span>@<span class="nt"></span></span>
<span class="nt"><input</span> <span class="na">type=</span><span class="s">"text"</span> <span class="na">pattern=</span><span class="s">"^[_A-z0-9]{1,}$"</span> <span class="na">maxlength=</span><span class="s">"15"</span> <span class="na">class=</span><span class="s">"form-control"</span> <span class="na">id=</span><span class="s">"inputTwitter"</span> <span class="na">placeholder=</span><span class="s">"1000hz"</span> <span class="na">required</span><span class="nt">></span>
<span class="nt"></div></span>
<span class="nt"><span</span> <span class="na">class=</span><span class="s">"glyphicon form-control-feedback"</span> <span class="na">aria-hidden=</span><span class="s">"true"</span><span class="nt">></span></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"help-block with-errors"</span><span class="nt">></span>Hey look, this one has feedback icons!<span class="nt"></div></span>
<span class="nt"></div></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"form-group"</span><span class="nt">></span>
<span class="nt"><label</span> <span class="na">for=</span><span class="s">"inputEmail"</span> <span class="na">class=</span><span class="s">"control-label"</span><span class="nt">></span>Email<span class="nt"></label></span>
<span class="nt"><input</span> <span class="na">type=</span><span class="s">"email"</span> <span class="na">class=</span><span class="s">"form-control"</span> <span class="na">id=</span><span class="s">"inputEmail"</span> <span class="na">placeholder=</span><span class="s">"Email"</span> <span class="na">data-error=</span><span class="s">"Bruh, that email address is invalid"</span> <span class="na">required</span><span class="nt">></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"help-block with-errors"</span><span class="nt">></div></span>
<span class="nt"></div></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"form-group"</span><span class="nt">></span>
<span class="nt"><label</span> <span class="na">for=</span><span class="s">"inputPassword"</span> <span class="na">class=</span><span class="s">"control-label"</span><span class="nt">></span>Password<span class="nt"></label></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"form-inline row"</span><span class="nt">></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"form-group col-sm-6"</span><span class="nt">></span>
<span class="nt"><input</span> <span class="na">type=</span><span class="s">"password"</span> <span class="na">data-minlength=</span><span class="s">"6"</span> <span class="na">class=</span><span class="s">"form-control"</span> <span class="na">id=</span><span class="s">"inputPassword"</span> <span class="na">placeholder=</span><span class="s">"Password"</span> <span class="na">required</span><span class="nt">></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"help-block"</span><span class="nt">></span>Minimum of 6 characters<span class="nt"></div></span>
<span class="nt"></div></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"form-group col-sm-6"</span><span class="nt">></span>
<span class="nt"><input</span> <span class="na">type=</span><span class="s">"password"</span> <span class="na">class=</span><span class="s">"form-control"</span> <span class="na">id=</span><span class="s">"inputPasswordConfirm"</span> <span class="na">data-match=</span><span class="s">"#inputPassword"</span> <span class="na">data-match-error=</span><span class="s">"Whoops, these don't match"</span> <span class="na">placeholder=</span><span class="s">"Confirm"</span> <span class="na">required</span><span class="nt">></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"help-block with-errors"</span><span class="nt">></div></span>
<span class="nt"></div></span>
<span class="nt"></div></span>
<span class="nt"></div></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"form-group"</span><span class="nt">></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"radio"</span><span class="nt">></span>
<span class="nt"><label></span>
<span class="nt"><input</span> <span class="na">type=</span><span class="s">"radio"</span> <span class="na">name=</span><span class="s">"underwear"</span> <span class="na">required</span><span class="nt">></span>
Boxers
<span class="nt"></label></span>
<span class="nt"></div></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"radio"</span><span class="nt">></span>
<span class="nt"><label></span>
<span class="nt"><input</span> <span class="na">type=</span><span class="s">"radio"</span> <span class="na">name=</span><span class="s">"underwear"</span> <span class="na">required</span><span class="nt">></span>
Briefs
<span class="nt"></label></span>
<span class="nt"></div></span>
<span class="nt"></div></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"form-group"</span><span class="nt">></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"checkbox"</span><span class="nt">></span>
<span class="nt"><label></span>
<span class="nt"><input</span> <span class="na">type=</span><span class="s">"checkbox"</span> <span class="na">id=</span><span class="s">"terms"</span> <span class="na">data-error=</span><span class="s">"Before you wreck yourself"</span> <span class="na">required</span><span class="nt">></span>
Check yourself
<span class="nt"></label></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"help-block with-errors"</span><span class="nt">></div></span>
<span class="nt"></div></span>
<span class="nt"></div></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"form-group"</span><span class="nt">></span>
<span class="nt"><button</span> <span class="na">type=</span><span class="s">"submit"</span> <span class="na">class=</span><span class="s">"btn btn-primary"</span><span class="nt">></span>Submit<span class="nt"></button></span>
<span class="nt"></div></span>
<span class="nt"></form></span></code></pre></figure>
<h2 id="validator-usage">Usage</h2>
Form validation can be enabled in markup via the data-api or via JavaScript.
Automatically enable form validation by adding <code>data-toggle="validator"</code> to your form element.
<figure class="highlight"><pre><code class="language-html" data-lang="html"><span class="nt"><form</span> <span class="na">role=</span><span class="s">"form"</span> <span class="na">data-toggle=</span><span class="s">"validator"</span><span class="nt">></span>
...
<span class="nt"></form></span></code></pre></figure>
Or activate validation via JavaScript:
<figure class="highlight"><pre><code class="language-js" data-lang="js"><span class="nx">$</span><span class="p">(</span><span class="s1">'#myForm'</span><span class="p">).</span><span class="nx">validator</span><span class="p">()</span></code></pre></figure>
<h3 id="validator-markup">Markup</h3>
<p>Follow Bootstrap's <a href="http://getbootstrap.com/css/#forms" target="_blank">examples</a> for appropriate form markup. It's important that each input field is in its own individual <code>.form-group</code> container for error messages to appear properly.</p>
<p>Validation rules are specified on form inputs via the following standard HTML5 attributes:</p>
<ul>
<li><code>type="email"</code></li>
<li><code>type="url"</code></li>
<li><code>type="number"</code>, with additional constraints via <code>max</code>, <code>min</code> and <code>step</code> attributes</li>
<li><code>pattern="Reg(ular )?Exp(ression)?"</code> (for input types of <code>text</code>, <code>search</code>, <code>tel</code>, <code>url</code> or <code>email</code>)</li>
<li><code>required</code></li>
</ul>
<p>As well as the following non-standard attributes:</p>
<ul>
<li><code>data-match="#inputToMatch"</code> to ensure two fields match, e.g. password confirmations</li>
<li><code>data-minlength="5"</code> to enforce a minimum amount of characters</li>
<li><code>data-remote="/path/to/remote/validator"</code> to make an AJAX request to determine if the field is valid or not. Be sure to give the input a <code>name</code> attribute, as the request will be sent to <code>/path/to/remote/validator?<name>=<value></code>. The remote endpoint should return a <code>200 OK</code> if the field is valid, and a <code>4xx</code> otherwise. Here's a <a href="https://github.com/1000hz/validator-remote-example">reference server implementation</a> using Express.</li>
</ul>
<div class="bs-callout bs-callout-info">
<h4>Standard Attribute Validators</h4>
<p>The validation rules for standard HTML5 attributes are handled entirely by the browser using the <a href="https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5/Constraint_validation">HTML5 Constraint Validation API</a>. As such, this plugin isn't in control of things like what qualifies as a valid email address or URL. If you find you need more restrictive validations for these fields, you can use the <code>pattern</code> attribute to further constrain what's acceptable.</p>
<p>Be careful that you aren't too restrictive though, which might lead to false negatives and a poor user experience. For example, you'd be surprised at what kinds of <a href="https://en.wikipedia.org/wiki/Email_address#Valid_email_addresses">email addresses</a> are considered valid according to standards.</p>
</div>
<div class="bs-callout bs-callout-danger">
<h4>Cross-browser Compatibility</h4>
<p>Because this plugin depends on the HTML5 Constraint Validation API, Internet Explorer 9 and older are not supported. If you need to support these browsers, you must add a polyfill like Ryan Seddon's <a href="https://github.com/ryanseddon/H5F" target="_blank">H5F</a>.</p>
</div>
<p>To display error messages, include a container after the input field with both <code>help-block</code> and <code>with-errors</code> classes.</p>
<figure class="highlight"><pre><code class="language-html" data-lang="html"><span class="nt"><form</span> <span class="na">role=</span><span class="s">"form"</span> <span class="na">data-toggle=</span><span class="s">"validator"</span><span class="nt">></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"form-group"</span><span class="nt">></span>
<span class="nt"><label</span> <span class="na">for=</span><span class="s">"inputEmail"</span><span class="nt">></span>Email<span class="nt"></label></span>
<span class="nt"><input</span> <span class="na">type=</span><span class="s">"email"</span> <span class="na">id=</span><span class="s">"inputEmail"</span><span class="nt">></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"help-block with-errors"</span><span class="nt">></div></span>
<span class="nt"></div></span>
<span class="nt"></form></span></code></pre></figure>
<h3 id="validator-fields">Validated fields</h3>
<p>By default, the validator will only validate fields that are present when the plugin is initialized. If your form has a dynamic set of fields, you will need to call <code>$(...).validator('update')</code> to inform the plugin that the set of fields to be validated has changed.</p>
<p>The default selector used to determine which fields are validated is:</p>
<figure class="highlight"><pre><code class="language-js" data-lang="js"><span class="nx">$</span><span class="p">.</span><span class="nx">fn</span><span class="p">.</span><span class="nx">validator</span><span class="p">.</span><span class="nx">Constructor</span><span class="p">.</span><span class="nx">INPUT_SELECTOR</span> <span class="o">=</span> <span class="s1">':input:not([type="hidden"], [type="submit"], [type="reset"], button)'</span></code></pre></figure>
<p>You can override this value from within your code if you need to change this default behavior. Alternatively, you can add <code>data-validate="true"</code> / <code>data-validate="false"</code> to a specific input to force its inclusion / exclusion in the set of validated fields.</p>
</div>
<h3 id="validator-options">Options</h3>
<p>Options can be passed via data attributes or JavaScript. For data attributes, append the option name to <code>data-</code>, as in <code>data-delay=""</code>.</p>
<div class="table-responsive">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th style="width: 100px;">Name</th>
<th style="width: 100px;">type</th>
<th style="width: 50px;">default</th>
<th>description</th>
</tr>
</thead>
<tbody>
<tr>
<td>delay</td>
<td>number</td>
<td>500</td>
<td>Number of milliseconds to wait before displaying an error on a form field.</td>
</tr>
<tr>
<td>html</td>
<td>boolean</td>
<td>false</td>
<td>Insert HTML into the error messages. If false, jQuery's text method will be used to insert content into the DOM. Use text if you're worried about XSS attacks.</td>
</tr>
<tr>
<td>disable</td>
<td>boolean</td>
<td>true</td>
<td>Disable the submit button until the form is valid and all required fields are complete.</td>
</tr>
<tr>
<td>focus</td>
<td>boolean</td>
<td>true</td>
<td>
<p>Scroll to and focus the first field with an error upon validation of the form.</p>
<p>If, for example, you have a fixed navbar at the top of your page and you need to adjust the amount of padding between the top of the window and the focused field, you can override the following variable:</p>
<figure class="highlight"><pre><code class="language-js" data-lang="js"><span class="nx">$</span><span class="p">.</span><span class="nx">fn</span><span class="p">.</span><span class="nx">validator</span><span class="p">.</span><span class="nx">Constructor</span><span class="p">.</span><span class="nx">FOCUS_OFFSET</span></code></pre></figure>
<p>It defaults to 20 (px).</p>
</td>
</tr>
<tr>
<td>feedback</td>
<td>object</td>
<td>glyphicon classes</td>
<td>
<p>Override the classes used for form feedback icons. Defaults to Bootstrap's glyphicons:</p>
<figure class="highlight"><pre><code class="language-js" data-lang="js"><span class="nx">feedback</span><span class="o">:</span> <span class="p">{</span>
<span class="nx">success</span><span class="o">:</span> <span class="s1">'glyphicon-ok'</span><span class="p">,</span>
<span class="nx">error</span><span class="o">:</span> <span class="s1">'glyphicon-remove'</span>
<span class="p">}</span></code></pre></figure>
</td>
</tr>
<tr>
<td>custom</td>
<td>object</td>
<td>{}</td>
<td>
<p>Add custom validators to be run. Validators should be functions that receive the jQuery element as an argument and return an error message if the field is invalid.</p>
<p>Here's an example of a custom validator that checks that an input is equal to some specified value:<p>
<figure class="highlight"><pre><code class="language-js" data-lang="js"><span class="nx">custom</span><span class="o">:</span> <span class="p">{</span>
<span class="nx">equals</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">$el</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">matchValue</span> <span class="o">=</span> <span class="nx">$el</span><span class="p">.</span><span class="nx">data</span><span class="p">(</span><span class="s2">"equals"</span><span class="p">)</span> <span class="c1">// foo</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">$el</span><span class="p">.</span><span class="nx">val</span><span class="p">()</span> <span class="o">!==</span> <span class="nx">matchValue</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="s2">"Hey, that's not valid! It's gotta be "</span> <span class="o">+</span> <span class="nx">matchValue</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span></code></pre></figure>
<p>Adding the validator to an input is done just like the others, by referencing its name as a data attribute: <code><input data-equals="foo"></code>. In this case, the field will display an error if the user enters anything other than <code>foo</code>.</p>
</td>
</tr>
</tbody>
</table>
</div><!-- /.table-responsive -->
<div class="bs-callout bs-callout-info">
<h4>Error messages for individual form fields</h4>
<p>Error messages for individual form fields can alternatively be specified through the use of data attributes. You can specify an error message for each type of validator on a field, i.e. <code>data-pattern-error=""</code>, <code>data-required-error=""</code>, <code>data-match-error=""</code>, etc... or use <code>data-error=""</code> for a blanket error message to show for any errors on that field.</p>
</div>
<h3 id="validator-methods"> Methods</h3>
<h4><code>.validator(options)</code></h4>
<p>Attaches a validator to a form collection.</p>
<h4><code>.validator('update')</code></h4>
<p>Updates the collection of inputs that will be validated. Call this method if the set of fields which need to be validated has changed.</p>
<h4><code>.validator('validate')</code></h4>
<p>Immediately validates the entire form.</p>
<h4><code>.validator('destroy')</code></h4>
<p>Destroys form validator and cleans up data left behind.</p>
<h3 id="validator-events">Events</h3>
<p>All events are fired on the form element and provide a reference to the form field to which the event pertains via <code>event.relatedTarget</code>.</p>
<div class="table-responsive">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th style="width: 150px;">Event Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>validate.bs.validator</td>
<td>This event fires immediately when a form field is validated.</td>
</tr>
<tr>
<td>invalid.bs.validator</td>
<td>This event is fired when a form field becomes invalid. Field errors are provided via <code>event.detail</code>.</td>
</tr>
<tr>
<td>valid.bs.validator</td>
<td>This event is fired when a form field becomes valid. Previous field errors are provided via <code>event.detail</code>.</td>
</tr>
<tr>
<td>validated.bs.validator</td>
<td>This event is fired after a form field has been validated.</td>
</tr>
</tbody>
</table>
</div><!-- ./bs-table-responsive -->
<div class="bs-callout bs-callout-info">
<h4>Conditionally handling the submit event</h4>
<p>When the form is invalid, <code>.preventDefault()</code> is called on the submit event. As a result, if you want to hook into the submit event and do something conditionally based on whether or not the form was valid, you can check if the event <code>.isDefaultPrevented()</code>. Be sure your submit handler is bound after the plugin has been initialized on your form.</p>
</div>
<figure class="highlight"><pre><code class="language-js" data-lang="js"><span class="nx">$</span><span class="p">(</span><span class="s1">'#form'</span><span class="p">).</span><span class="nx">validator</span><span class="p">().</span><span class="nx">on</span><span class="p">(</span><span class="s1">'submit'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">isDefaultPrevented</span><span class="p">())</span> <span class="p">{</span>
<span class="c1">// handle the invalid form...</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="c1">// everything looks good!</span>
<span class="p">}</span>
<span class="p">})</span></code></pre></figure>
</div>
</div>
</div>
</div>
<!-- Footer
================================================== -->
<footer class="bs-footer" role="contentinfo">
<div class="container">
<div class="bs-social">
<ul class="bs-social-buttons">
<li>
<iframe class="github-btn" src="http://ghbtns.com/github-btn.html?user=1000hz&repo=bootstrap-validator&type=watch&count=true" width="100" height="20" title="Star on GitHub"></iframe>
</li>
<li>
<iframe class="github-btn" src="http://ghbtns.com/github-btn.html?user=1000hz&repo=bootstrap-validator&type=fork&count=true" width="102" height="20" title="Fork on GitHub"></iframe>
</li>
<li class="follow-btn">
<a href="https://twitter.com/1000hz" class="twitter-follow-button" data-link-color="#0069D6" data-show-count="true">Follow @1000hz</a>
</li>
</ul>
</div>
<p>Built by <a href="http://twitter.com/1000hz" target="_blank">@1000hz</a> with all the love in the world for <a href="http://twitter.com/mdo" target="_blank">@mdo</a> and <a href="http://twitter.com/fat" target="_blank">@fat</a>.</p>
<p>This project is not affiliated with Bootstrap in any official capacity.</p>
<p>This documentation page has been adapted from Bootstrap's official docs.</p>
<p>Code licensed under <a href="http://opensource.org/licenses/MIT" target="_blank">MIT License</a>, documentation under <a href="http://creativecommons.org/licenses/by/3.0/">CC BY 3.0</a>.</p>
</div>
</footer>
<!-- JS and analytics only. -->
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="dist/validator.min.js"></script>
<script src="http://platform.twitter.com/widgets.js"></script>
<script src="assets/js/application.js"></script>
</body>
</html>