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