Skip to content

Instantly share code, notes, and snippets.

@cfjedimaster
Created January 31, 2013 16:13
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save cfjedimaster/4683989 to your computer and use it in GitHub Desktop.
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.0.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/jquery.validate.js"></script>
<script>
$(document).ready(function(){
$("#commentForm").validate({
submitHandler: function(form){
var fields = $("input.required");
var total = 0;
fields.each(function(x,item) {
total += parseInt($(this).val());
});
console.log(total);
if (total != 100) {
$("#commentForm div.error").html("Your percantage fields must sum to 100.")
return false;
} else form.submit();
}
});
});
</script>
</head>
<body>
<form id="commentForm" method="get" action="">
<fieldset>
<legend>A simple comment form with submit validation and default messages</legend>
<p>
<label for="pA">Percent A</label>
<em>*</em><input id="pA" name="pA" size="2" type="number" class="required numeric" min="1" max="100" />
</p>
<p>
<label for="pB">Percent B</label>
<em>*</em><input id="pB" name="pB" size="2" type="number" class="required numeric" min="1" max="100" />
</p>
<p>
<label for="pC">Percent C</label>
<em>*</em><input id="pC" name="pC" size="2" type="number" class="required numeric" min="1" max="100" />
</p>
<p>
<input class="submit" type="submit" value="Submit"/>
</p>
</fieldset>
<div class="error"></div>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment