| 2 |
lars |
1 |
<!DOCTYPE html>
|
|
|
2 |
<html lang="en">
|
|
|
3 |
<head>
|
|
|
4 |
<meta charset="utf-8">
|
|
|
5 |
<title>jQuery validation plug-in - dynamic forms demo</title>
|
|
|
6 |
<link rel="stylesheet" media="screen" href="css/screen.css">
|
|
|
7 |
<script src="../lib/jquery.js"></script>
|
|
|
8 |
<script src="../dist/jquery.validate.js"></script>
|
|
|
9 |
<script>
|
|
|
10 |
// only for demo purposes
|
|
|
11 |
$.validator.setDefaults({
|
|
|
12 |
submitHandler: function() {
|
|
|
13 |
alert("submitted!");
|
|
|
14 |
}
|
|
|
15 |
});
|
|
|
16 |
$.validator.messages.max = jQuery.validator.format("Your totals mustn't exceed {0}!");
|
|
|
17 |
|
|
|
18 |
$.validator.addMethod("quantity", function(value, element) {
|
|
|
19 |
return !this.optional(element) && !this.optional($(element).parent().prev().children("select")[0]);
|
|
|
20 |
}, "Please select both the item and its amount.");
|
|
|
21 |
|
|
|
22 |
$().ready(function() {
|
|
|
23 |
$("#orderform").validate({
|
|
|
24 |
errorPlacement: function(error, element) {
|
|
|
25 |
error.appendTo(element.parent().next());
|
|
|
26 |
},
|
|
|
27 |
highlight: function(element, errorClass) {
|
|
|
28 |
$(element).addClass(errorClass).parent().prev().children("select").addClass(errorClass);
|
|
|
29 |
}
|
|
|
30 |
});
|
|
|
31 |
|
|
|
32 |
var template = jQuery.validator.format($.trim($("#template").val()));
|
|
|
33 |
|
|
|
34 |
function addRow() {
|
|
|
35 |
$(template(i++)).appendTo("#orderitems tbody");
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
var i = 1;
|
|
|
39 |
// start with one row
|
|
|
40 |
addRow();
|
|
|
41 |
// add more rows on click
|
|
|
42 |
$("#add").click(addRow);
|
|
|
43 |
|
|
|
44 |
// check keyup on quantity inputs to update totals field
|
|
|
45 |
$("#orderform").on("keyup", "input.quantity", function(event) {
|
|
|
46 |
var totals = 0;
|
|
|
47 |
$("#orderitems input.quantity").each(function() {
|
|
|
48 |
totals += +this.value;
|
|
|
49 |
});
|
|
|
50 |
$("#totals").attr("value", totals).valid();
|
|
|
51 |
});
|
|
|
52 |
|
|
|
53 |
});
|
|
|
54 |
</script>
|
|
|
55 |
<style>
|
|
|
56 |
form.cmxform {
|
|
|
57 |
width: 50em;
|
|
|
58 |
}
|
|
|
59 |
em.error {
|
|
|
60 |
background:url("images/unchecked.gif") no-repeat 0px 0px;
|
|
|
61 |
padding-left: 16px;
|
|
|
62 |
}
|
|
|
63 |
em.success {
|
|
|
64 |
background:url("images/checked.gif") no-repeat 0px 0px;
|
|
|
65 |
padding-left: 16px;
|
|
|
66 |
}
|
|
|
67 |
form.cmxform label.error {
|
|
|
68 |
margin-left: auto;
|
|
|
69 |
width: 250px;
|
|
|
70 |
}
|
|
|
71 |
form.cmxform input.submit {
|
|
|
72 |
margin-left: 0;
|
|
|
73 |
}
|
|
|
74 |
em.error {
|
|
|
75 |
color: black;
|
|
|
76 |
}
|
|
|
77 |
#warning {
|
|
|
78 |
display: none;
|
|
|
79 |
}
|
|
|
80 |
select.error {
|
|
|
81 |
border: 1px dotted red;
|
|
|
82 |
}
|
|
|
83 |
</style>
|
|
|
84 |
</head>
|
|
|
85 |
<body>
|
|
|
86 |
<h1 id="banner"><a href="https://jqueryvalidation.org/">jQuery Validation Plugin</a> Demo</h1>
|
|
|
87 |
<div id="main">
|
|
|
88 |
<textarea style="display:none" id="template">
|
|
|
89 |
<tr>
|
|
|
90 |
<td>
|
|
|
91 |
<label>{0}. Item</label>
|
|
|
92 |
</td>
|
|
|
93 |
<td class='type'>
|
|
|
94 |
<select name="item-type-{0}">
|
|
|
95 |
<option value="">Select...</option>
|
|
|
96 |
<option value="0">Learning jQuery</option>
|
|
|
97 |
<option value="1">jQuery Reference Guide</option>
|
|
|
98 |
<option value="2">jQuery Cookbook</option>
|
|
|
99 |
<option vlaue="3">jQuery In Action</option>
|
|
|
100 |
<option value="4">jQuery For Designers</option>
|
|
|
101 |
</select>
|
|
|
102 |
</td>
|
|
|
103 |
<td class='quantity'>
|
|
|
104 |
<input size='4' class="quantity" min="1" id="item-quantity-{0}" name="item-quantity-{0}">
|
|
|
105 |
</td>
|
|
|
106 |
<td class='quantity-error'></td>
|
|
|
107 |
</tr>
|
|
|
108 |
</textarea>
|
|
|
109 |
<form id="orderform" class="cmxform" method="get" action="foo.html">
|
|
|
110 |
<h2 id="summary"></h2>
|
|
|
111 |
<fieldset>
|
|
|
112 |
<legend>Example with custom methods and heavily customized error display</legend>
|
|
|
113 |
<table id="orderitems">
|
|
|
114 |
<tbody>
|
|
|
115 |
</tbody>
|
|
|
116 |
<tfoot>
|
|
|
117 |
<tr>
|
|
|
118 |
<td colspan="2">
|
|
|
119 |
<label>Totals (max 25)</label>
|
|
|
120 |
</td>
|
|
|
121 |
<td class="totals">
|
|
|
122 |
<input id="totals" name="totals" value="0" max="25" readonly="readonly" size='4'>
|
|
|
123 |
</td>
|
|
|
124 |
<td class="totals-error"></td>
|
|
|
125 |
</tr>
|
|
|
126 |
<tr>
|
|
|
127 |
<td colspan="2"> </td>
|
|
|
128 |
<td>
|
|
|
129 |
<input class="submit" type="submit" value="Submit">
|
|
|
130 |
</td>
|
|
|
131 |
</tr>
|
|
|
132 |
</tfoot>
|
|
|
133 |
</table>
|
|
|
134 |
</fieldset>
|
|
|
135 |
</form>
|
|
|
136 |
<button id="add">Add another input to the form</button>
|
|
|
137 |
<h1 id="warning">Your form contains tons of errors! Please try again.</h1>
|
|
|
138 |
<p><a href="index.html">Back to main page</a>
|
|
|
139 |
</p>
|
|
|
140 |
</div>
|
|
|
141 |
</body>
|
|
|
142 |
</html>
|