-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.html
More file actions
264 lines (232 loc) · 10.7 KB
/
Copy pathsample.html
File metadata and controls
264 lines (232 loc) · 10.7 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Validate Demo | aLittleCode</title>
<meta name="viewport" content="width=device-width">
<link href="style.css" rel="stylesheet">
<script src="js/modernizr-2.5.3.min.js"></script>
</head>
<body>
<div class="container">
<div class="page-header">
<h1>jQuery Validate Demo</h1>
<p>with styles from Twitter Bootstrap</p>
<div class="alert alert-info"><p>Now updated to remove or add error or success classes when user changes a field. (<a href="http://alittlecode.com/jquery-form-validation-with-styles-from-twitter-bootstrap/#comment-461">Thanks Giedrius</a>!)</p></div>
</div>
<div class="row">
<div id="maincontent" class="span8">
<div class="tabbable">
<ul class="nav nav-pills">
<li class="active"><a href="#demo" data-toggle="tab">Demo</a></li>
<li><a href="#html" data-toggle="tab">the HTML</a></li>
<li><a href="#css" data-toggle="tab">the CSS</a></li>
<li><a href="#jquery" data-toggle="tab">the jQuery</a></li>
</ul>
<div class="tab-content">
<div id="demo" class="tab-pane active">
<div class="alert alert-success">
<h4>NOTES</h4>
<ul>
<li>To receive feedback, fill in a field and tab to the next. To get negative feedback, only enter one character.</li>
<li>For explanations, see the tabs above for the code, and of course <a href="http://docs.jquery.com/Plugins/Validation/"><strong>check out the plugin documentation</strong></a>.</li>
</ul>
</div><!-- notes .alert -->
<form action="" id="contact-form" class="form-horizontal">
<fieldset>
<legend>Sample Contact Form <small>(will not submit any information)</small></legend>
<div class="control-group">
<label class="control-label" for="name">Your Name</label>
<div class="controls">
<input type="text" class="input-xlarge" name="name" id="name">
</div>
</div>
<div class="control-group">
<label class="control-label" for="email">Email Address</label>
<div class="controls">
<input type="text" class="input-xlarge" name="email" id="email">
</div>
</div>
<div class="control-group">
<label class="control-label" for="subject">Subject</label>
<div class="controls">
<input type="text" class="input-xlarge" name="subject" id="subject">
</div>
</div>
<div class="control-group">
<label class="control-label" for="message">Your Message</label>
<div class="controls">
<textarea class="input-xlarge" name="message" id="message" rows="3"></textarea>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary btn-large">Submit</button>
<button type="reset" class="btn">Cancel</button>
</div>
</fieldset>
</form>
</div><!-- .tab-pane -->
<div id="html" class="tab-pane">
<h3>the HTML</h3>
<p>Form elements with classes for Twitter Bootstrap styles and layout.</p>
<pre><form action="" id="contact-form" class="form-horizontal">
<fieldset>
<legend>Sample Contact Form <small>(will not submit any information)</small></legend>
<div class="control-group">
<label class="control-label" for="name">Your Name</label>
<div class="controls">
<input type="text" class="input-xlarge" name="name" id="name">
</div>
</div>
<div class="control-group">
<label class="control-label" for="email">Email Address</label>
<div class="controls">
<input type="text" class="input-xlarge" name="email" id="email">
</div>
</div>
<div class="control-group">
<label class="control-label" for="subject">Subject</label>
<div class="controls">
<input type="text" class="input-xlarge" name="subject" id="subject">
</div>
</div>
<div class="control-group">
<label class="control-label" for="message">Your Message</label>
<div class="controls">
<textarea class="input-xlarge" name="message" id="message" rows="3"></textarea>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary btn-large">Submit</button>
<button type="reset" class="btn">Cancel</button>
</div>
</fieldset>
</form></pre>
</div><!-- .tab-pane -->
<div id="css" class="tab-pane">
<h3>the CSS</h3>
<p>Found in <code>style.css</code>, these styles correspond to the labels generated by the Validate Plugin and provide the green check mark on success and the red message color on error.</p>
<p>Note that Twitter Bootstrap (v.2.0.2) comes with styles for "success" and "error" that apply green or red, respectively, to the entire control group.</p>
<p>The green checkmark icon (here named <code>valid.png</code>) is not a part of Twitter Bootstrap <a href="http://webdesign.tutsplus.com/freebies/exclusive-freebie-the-kudos-web-icon-set/">but rather from the free Kudos icon set</a>.</p>
<pre>label.valid {
width: 24px;
height: 24px;
background: url(assets/img/valid.png) center center no-repeat;
display: inline-block;
text-indent: -9999px;
}
label.error {
font-weight: bold;
color: red;
padding: 2px 8px;
margin-top: 2px;
}</pre>
</div><!-- .tab-pane -->
<div id="jquery" class="tab-pane">
<h3>the jQuery</h3>
<h4>Setup</h4>
<p>Link jQuery and the Validate Plugin at bottom of the html file, like so:</p>
<pre><!-- jQuery -->
<script src="assets/js/jquery-1.7.1.min.js"></script>
<!-- Validate Plugin -->
<script src="assets/js/jquery.validate.min.js"></script></pre>
<h4>Initialize and Set Options</h4>
<p>You'll find <a href="http://docs.jquery.com/Plugins/Validation/validate#toptions">other options at the plugin's documentation page</a>.</p>
<p>Note that Twitter Bootstrap (v.2.0.2) comes with styles for the classes "success" and "error." I've added these classes to the closest parent control group using the success and highlight options below.</p>
<pre>$(document).ready(function(){
$('#contact-form').validate(
{
rules: {
name: {
minlength: 2,
required: true
},
email: {
required: true,
email: true
},
subject: {
minlength: 2,
required: true
},
message: {
minlength: 2,
required: true
}
},
highlight: function(element) {
$(element).closest('.control-group').removeClass('success').addClass('error');
},
success: function(element) {
element
.text('OK!').addClass('valid')
.closest('.control-group').removeClass('error').addClass('success');
}
});
}); // end document.ready</pre>
</div><!-- .tab-pane -->
</div><!-- .tab-content -->
</div><!-- .tabbable -->
</div><!-- .span -->
<div id="sidebar" class="offset1 span3">
<div class="well">
<h2>Links</h2>
<a href="http://alittlecode.com/">« Back to aLittleCode.com</a>
<h3>jQuery Validate Plugin</h3>
<ul>
<li><a href="http://docs.jquery.com/Plugins/Validation/">jQuery Validate Plugin</a></li>
<li><a href="http://docs.jquery.com/Plugins/Validation/validate#toptions">Plugin Options</a></li>
<li><a href="http://bassistance.de/jquery-plugins/jquery-plugin-validation/">Author's Plugin Page</a></li>
<li><a href="https://github.com/jzaefferer/jquery-validation">Github Repository</a></li>
</ul>
<h3>Other Assets</h3>
<ul>
<li><a href="http://webdesign.tutsplus.com/freebies/exclusive-freebie-the-kudos-web-icon-set/">Kudos Web Icons (free)</a></li>
<li><a href="http://twitter.github.com/bootstrap/">Twitter Bootstrap</a></li>
</ul>
<h3>Other Credits</h3>
<p>This demo builds upon an example provided in the excellent (and recommended) <a href="http://www.sitepoint.com/books/jquery2/">jQuery Novice to Ninja</a>.</p>
</div><!-- .well -->
<h3>Download</h3>
<p>Grab a working sample:</p>
<a class="btn btn-primary" href="http://alittlecode.com/files/jQuery-Validate-Example-COMPLETE.zip">jQuery-Validate-Example.zip <i class="icon-download icon-white"></i></a>
</div><!-- .span -->
</div><!-- .row -->
<hr>
<div class="alert alert-info"><p><a href="http://alittlecode.com/jquery-form-validation-with-styles-from-twitter-bootstrap/#comments">Care to leave a comment?</a></p></div>
<footer>
<p>By David Cochran of <a href="http://alittlecode.com/">aLittleCode.com</a>. Free for your use! (No warranties, no guarantees.)</p>
</footer>
</div> <!-- .container -->
<!-- ==============================================
JavaScript below! -->
<!-- jQuery via Google + local fallback, see h5bp.com -->
<script src="assets/js/jquery-1.7.1.min.js"></script>
<!-- Bootstrap JS -->
<script src="assets/js/bootstrap.min.js"></script>
<!-- Validate plugin -->
<script src="assets/js/jquery.validate.min.js"></script>
<!-- Prettify plugin -->
<script src="assets/js/prettify/prettify.js"></script>
<!-- Scripts specific to this page -->
<script src="script.js"></script>
<script>
// Activate Google Prettify in this page
addEventListener('load', prettyPrint, false);
$(document).ready(function(){
// Add prettyprint class to pre elements
$('pre').addClass('prettyprint linenums');
});
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-22151549-3']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</body>
</html>