| 2 |
lars |
1 |
<!DOCTYPE html>
|
|
|
2 |
<html>
|
|
|
3 |
<head>
|
|
|
4 |
<meta charset="utf-8">
|
|
|
5 |
<title>jQuery validation plug-in - ThemeRolldered demo</title>
|
|
|
6 |
<link rel="stylesheet" media="screen" href="https://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.min.css">
|
|
|
7 |
<style>
|
|
|
8 |
body {
|
|
|
9 |
font-size: 62.5%;
|
|
|
10 |
}
|
|
|
11 |
label {
|
|
|
12 |
display: inline-block;
|
|
|
13 |
width: 100px;
|
|
|
14 |
}
|
|
|
15 |
legend {
|
|
|
16 |
padding: 0.5em;
|
|
|
17 |
}
|
|
|
18 |
fieldset fieldset label {
|
|
|
19 |
display: block;
|
|
|
20 |
}
|
|
|
21 |
#commentForm {
|
|
|
22 |
width: 500px;
|
|
|
23 |
}
|
|
|
24 |
#commentForm label {
|
|
|
25 |
width: 250px;
|
|
|
26 |
}
|
|
|
27 |
#commentForm label.error, #commentForm button.submit {
|
|
|
28 |
margin-left: 253px;
|
|
|
29 |
}
|
|
|
30 |
#signupForm {
|
|
|
31 |
width: 670px;
|
|
|
32 |
}
|
|
|
33 |
#signupForm label.error {
|
|
|
34 |
margin-left: 10px;
|
|
|
35 |
width: auto;
|
|
|
36 |
display: inline;
|
|
|
37 |
}
|
|
|
38 |
#newsletter_topics label.error {
|
|
|
39 |
display: none;
|
|
|
40 |
margin-left: 103px;
|
|
|
41 |
}
|
|
|
42 |
</style>
|
|
|
43 |
</head>
|
|
|
44 |
<body>
|
|
|
45 |
<form class="cmxform" id="commentForm" method="get" action="">
|
|
|
46 |
<fieldset class="ui-widget ui-widget-content ui-corner-all">
|
|
|
47 |
<legend class="ui-widget ui-widget-header ui-corner-all">Please provide your name, email address (won't be published) and a comment</legend>
|
|
|
48 |
<p>
|
|
|
49 |
<label for="cname">Name (required, at least 2 characters)</label>
|
|
|
50 |
<input id="cname" name="name" class="ui-widget-content" minlength="2" required type="text">
|
|
|
51 |
<p>
|
|
|
52 |
<label for="cemail">E-Mail (required)</label>
|
|
|
53 |
<input id="cemail" name="email" class="ui-widget-content" type="email" required>
|
|
|
54 |
</p>
|
|
|
55 |
<p>
|
|
|
56 |
<label for="curl">URL (optional)</label>
|
|
|
57 |
<input id="curl" name="url" class="ui-widget-content" value="" type="url">
|
|
|
58 |
</p>
|
|
|
59 |
<p>
|
|
|
60 |
<label for="ccomment">Your comment (required)</label>
|
|
|
61 |
<textarea id="ccomment" name="comment" class="ui-widget-content" required></textarea>
|
|
|
62 |
</p>
|
|
|
63 |
<p>
|
|
|
64 |
<button class="submit" type="submit">Submit</button>
|
|
|
65 |
</p>
|
|
|
66 |
</fieldset>
|
|
|
67 |
</form>
|
|
|
68 |
<form class="cmxform" id="signupForm" method="get" action="">
|
|
|
69 |
<fieldset class="ui-widget ui-widget-content ui-corner-all">
|
|
|
70 |
<legend class="ui-widget ui-widget-header ui-corner-all">Validating a complete form</legend>
|
|
|
71 |
<p>
|
|
|
72 |
<label for="firstname">Firstname</label>
|
|
|
73 |
<input id="firstname" name="firstname" type="text">
|
|
|
74 |
</p>
|
|
|
75 |
<p>
|
|
|
76 |
<label for="lastname">Lastname</label>
|
|
|
77 |
<input id="lastname" name="lastname" type="text">
|
|
|
78 |
</p>
|
|
|
79 |
<p>
|
|
|
80 |
<label for="username">Username</label>
|
|
|
81 |
<input id="username" name="username" type="text">
|
|
|
82 |
</p>
|
|
|
83 |
<p>
|
|
|
84 |
<label for="password">Password</label>
|
|
|
85 |
<input id="password" name="password" type="password">
|
|
|
86 |
</p>
|
|
|
87 |
<p>
|
|
|
88 |
<label for="confirm_password">Confirm password</label>
|
|
|
89 |
<input id="confirm_password" name="confirm_password" type="password">
|
|
|
90 |
</p>
|
|
|
91 |
<p>
|
|
|
92 |
<label for="email">Email</label>
|
|
|
93 |
<input id="email" name="email" type="email">
|
|
|
94 |
</p>
|
|
|
95 |
<p>
|
|
|
96 |
<label for="agree">Please agree to our policy</label>
|
|
|
97 |
<input type="checkbox" class="checkbox" id="agree" name="agree">
|
|
|
98 |
</p>
|
|
|
99 |
<p>
|
|
|
100 |
<label for="newsletter">I'd like to receive the newsletter</label>
|
|
|
101 |
<input type="checkbox" class="checkbox" id="newsletter" name="newsletter">
|
|
|
102 |
</p>
|
|
|
103 |
<fieldset id="newsletter_topics" class="ui-widget-content ui-corner-all">
|
|
|
104 |
<legend class="ui-widget-header ui-corner-all">Topics (select at least two) - note: would be hidden when newsletter isn't selected, but is visible here for the demo</legend>
|
|
|
105 |
<label for="topic_marketflash">
|
|
|
106 |
<input type="checkbox" id="topic_marketflash" value="marketflash" name="topic">Marketflash
|
|
|
107 |
</label>
|
|
|
108 |
<label for="topic_fuzz">
|
|
|
109 |
<input type="checkbox" id="topic_fuzz" value="fuzz" name="topic">Latest fuzz
|
|
|
110 |
</label>
|
|
|
111 |
<label for="topic_digester">
|
|
|
112 |
<input type="checkbox" id="topic_digester" value="digester" name="topic">Mailing list digester
|
|
|
113 |
</label>
|
|
|
114 |
<label for="topic" class="error">Please select at least two topics you'd like to receive.</label>
|
|
|
115 |
</fieldset>
|
|
|
116 |
<p>
|
|
|
117 |
<button class="submit" type="submit">Submit</button>
|
|
|
118 |
</p>
|
|
|
119 |
</fieldset>
|
|
|
120 |
</form>
|
|
|
121 |
<script src="../lib/jquery.js"></script>
|
|
|
122 |
<script src="../dist/jquery.validate.js"></script>
|
|
|
123 |
<script src="https://code.jquery.com/ui/1.10.4/jquery-ui.min.js"></script>
|
|
|
124 |
<script>
|
|
|
125 |
$.validator.setDefaults({
|
|
|
126 |
submitHandler: function() {
|
|
|
127 |
alert("submitted!");
|
|
|
128 |
},
|
|
|
129 |
showErrors: function(map, list) {
|
|
|
130 |
// there's probably a way to simplify this
|
|
|
131 |
var focussed = document.activeElement;
|
|
|
132 |
if (focussed && $(focussed).is("input, textarea")) {
|
|
|
133 |
$(this.currentForm).tooltip("close", {
|
|
|
134 |
currentTarget: focussed
|
|
|
135 |
}, true)
|
|
|
136 |
}
|
|
|
137 |
this.currentElements.removeAttr("title").removeClass("ui-state-highlight");
|
|
|
138 |
$.each(list, function(index, error) {
|
|
|
139 |
$(error.element).attr("title", error.message).addClass("ui-state-highlight");
|
|
|
140 |
});
|
|
|
141 |
if (focussed && $(focussed).is("input, textarea")) {
|
|
|
142 |
$(this.currentForm).tooltip("open", {
|
|
|
143 |
target: focussed
|
|
|
144 |
});
|
|
|
145 |
}
|
|
|
146 |
}
|
|
|
147 |
});
|
|
|
148 |
|
|
|
149 |
(function() {
|
|
|
150 |
// use custom tooltip; disable animations for now to work around lack of refresh method on tooltip
|
|
|
151 |
$("#commentForm, #signupForm").tooltip({
|
|
|
152 |
show: false,
|
|
|
153 |
hide: false
|
|
|
154 |
});
|
|
|
155 |
|
|
|
156 |
// validate the comment form when it is submitted
|
|
|
157 |
$("#commentForm").validate();
|
|
|
158 |
|
|
|
159 |
// validate signup form on keyup and submit
|
|
|
160 |
$("#signupForm").validate({
|
|
|
161 |
rules: {
|
|
|
162 |
firstname: "required",
|
|
|
163 |
lastname: "required",
|
|
|
164 |
username: {
|
|
|
165 |
required: true,
|
|
|
166 |
minlength: 2
|
|
|
167 |
},
|
|
|
168 |
password: {
|
|
|
169 |
required: true,
|
|
|
170 |
minlength: 5
|
|
|
171 |
},
|
|
|
172 |
confirm_password: {
|
|
|
173 |
required: true,
|
|
|
174 |
minlength: 5,
|
|
|
175 |
equalTo: "#password"
|
|
|
176 |
},
|
|
|
177 |
email: {
|
|
|
178 |
required: true,
|
|
|
179 |
email: true
|
|
|
180 |
},
|
|
|
181 |
topic: {
|
|
|
182 |
required: "#newsletter:checked",
|
|
|
183 |
minlength: 2
|
|
|
184 |
},
|
|
|
185 |
agree: "required"
|
|
|
186 |
},
|
|
|
187 |
messages: {
|
|
|
188 |
firstname: "Please enter your firstname",
|
|
|
189 |
lastname: "Please enter your lastname",
|
|
|
190 |
username: {
|
|
|
191 |
required: "Please enter a username",
|
|
|
192 |
minlength: "Your username must consist of at least 2 characters"
|
|
|
193 |
},
|
|
|
194 |
password: {
|
|
|
195 |
required: "Please provide a password",
|
|
|
196 |
minlength: "Your password must be at least 5 characters long"
|
|
|
197 |
},
|
|
|
198 |
confirm_password: {
|
|
|
199 |
required: "Please provide a password",
|
|
|
200 |
minlength: "Your password must be at least 5 characters long",
|
|
|
201 |
equalTo: "Please enter the same password as above"
|
|
|
202 |
},
|
|
|
203 |
email: "Please enter a valid email address",
|
|
|
204 |
agree: "Please accept our policy"
|
|
|
205 |
}
|
|
|
206 |
});
|
|
|
207 |
|
|
|
208 |
// propose username by combining first- and lastname
|
|
|
209 |
$("#username").focus(function() {
|
|
|
210 |
var firstname = $("#firstname").val();
|
|
|
211 |
var lastname = $("#lastname").val();
|
|
|
212 |
if (firstname && lastname && !this.value) {
|
|
|
213 |
this.value = firstname + "." + lastname;
|
|
|
214 |
}
|
|
|
215 |
});
|
|
|
216 |
|
|
|
217 |
//code to hide topic selection, disable for demo
|
|
|
218 |
var newsletter = $("#newsletter");
|
|
|
219 |
// newsletter topics are optional, hide at first
|
|
|
220 |
var inital = newsletter.is(":checked");
|
|
|
221 |
var topics = $("#newsletter_topics")[inital ? "removeClass" : "addClass"]("gray");
|
|
|
222 |
var topicInputs = topics.find("input").attr("disabled", !inital);
|
|
|
223 |
// show when newsletter is checked
|
|
|
224 |
newsletter.click(function() {
|
|
|
225 |
topics[this.checked ? "removeClass" : "addClass"]("gray");
|
|
|
226 |
topicInputs.attr("disabled", !this.checked);
|
|
|
227 |
});
|
|
|
228 |
|
|
|
229 |
$("#signupForm input:not(:submit)").addClass("ui-widget-content");
|
|
|
230 |
|
|
|
231 |
$(":submit").button();
|
|
|
232 |
})();
|
|
|
233 |
</script>
|
|
|
234 |
</body>
|
|
|
235 |
</html>
|