JQuery验证:如何添加验证它检查多个字段的总和? [英] JQuery Validate: How do I add validation which checks the sum of multiple fields?
问题描述
我试图使用jQuery验证了动态表单我设立。
在某些情况下,这种形式的包含了一组这是假设总计100输入框。
一个例子可能是:
请说明哪些学生比例是每级?结果
9级:TextBox1的结果
等级10:TextBox2中结果
11级:TextBox3结果
12年级:TextBox4结果
我想验证TextBox1中+ TextBox2中+ TextBox3 + TextBox4 = 100%。
我怎么去呢?
谢谢!
<脚本>
$(文件)。就绪(函数(){
$(#某种形式的)。验证({ 规则:{
TextBox1的:{
要求:真实,
编号:真,
分:0,
最大:100,
},
TextBox2中:{
要求:真实,
编号:真,
分:0,
最大:100,
}
TextBox3:{
要求:真实,
编号:真,
分:0,
最大:100,
}
TextBox4:{
要求:真实,
编号:真,
分:0,
最大:100,
} },
submitHandler:功能(形式){
无功总= parseInt函数($(#TextBox1中)。VAL())+ parseInt函数($(#TextBox2中)。VAL())+ parseInt函数($(#TextBox3)。VAL())+ parseInt函数( $(#TextBox4)VAL())。
如果(总!= 100){
$(错误)。HTML(你总总和必须100。)
返回false;
}其他form.submit();
} });
});< / SCRIPT>
被盗和编辑<一个href=\"http://www.coldfusionjedi.com/index.cfm/2009/3/26/Using-jQuery-to-validate-the-sum-of-form-fields\">from这里。
I'm trying to use jQuery validation for a dynamic form I'm setting up.
In some cases this form contains a set of input boxes which are suppose to total 100.
An example might be:
Please indicate what percentage of students are in each grade?
Grade 9: TextBox1
Grade 10: TextBox2
Grade 11: TextBox3
Grade 12: TextBox4
I want to verify that TextBox1 + TextBox2 + TextBox3 + TextBox4 = 100%.
How do I go about this?
Thanks!
<script>
$(document).ready(function(){
$("#some-form").validate({
rules: {
TextBox1: {
required: true,
number: true,
min: 0,
max: 100,
},
TextBox2 : {
required: true,
number: true,
min: 0,
max: 100,
}
TextBox3 : {
required: true,
number: true,
min: 0,
max: 100,
}
TextBox4 : {
required: true,
number: true,
min: 0,
max: 100,
}
},
submitHandler: function(form){
var total = parseInt($("#TextBox1").val()) + parseInt($("#TextBox2").val()) + parseInt($("#TextBox3").val()) + parseInt($("#TextBox4").val());
if (total != 100) {
$(".error").html("Your total must sum to 100.")
return false;
} else form.submit();
}
});
});
</script>
Stolen and edited from here.
这篇关于JQuery验证:如何添加验证它检查多个字段的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!