同时怎样校验多个控件的数字之和呢?
有3个TextBox,可以输入数字,但是3个控件的和必须为100,而且每个控件的值必须大于或等于0且小于100.
例如
TextBox1 = 30 , TextBox2 = 50 , TextBox3 = 20 (合法)
TextBox1 = 100 , TextBox2 = 0 , TextBox3 = 0 (合法)
TextBox1 = 30 , TextBox2 = 70 , TextBox3 = 0 (合法)
TextBox1 = 110 , TextBox2 = -10 , TextBox3 = 0 (非法)
TextBox1 = 0 , TextBox2 = 90 , TextBox3 = 0 (非法)
------解决方案--------------------使用CustomValidator的ClientValidationFunction即可
------解决方案--------------------用RangeValidator
设置最大和最小
------解决方案--------------------<asp:CustomValidator id="CustomValidator" runat="server" Display="None" Width="184px" ClientValidationFunction="CheckLength"></asp:CustomValidator>
function CheckLength(arguments)
{
if (a+b+c==100))
{
arguments.IsValid = true;
}
else
arguments.IsValid = false;
}
刚没注意看 .....
用RangeValidator
设置最大和最小
这样就可以
------解决方案--------------------<asp:CustomValidator id="CustomValidator" runat="server" Display="None" Width="184px" ClientValidationFunction="CheckLength" > </asp:CustomValidator >
function CheckLength(arguments)
{
if (a+b+c==100))
{
arguments.IsValid = true;
}
else
arguments.IsValid = false;
}
====================================
同意楼上的 !
------解决方案--------------------javascript才是王道~~
<script language="javascript">
<!--
//判断按键是否为数字
function onlyNumber(e)
{
var k = window.event.keyCode;
if (k < 48 || k > 57)
{
window.alert("请输入数字!");
window.event.keyCode = 0;
e.focus();
}
}
//客户端验证函数
function valid()
{
var t1 = parseInt(document.getElementById("TextBox1").value);
var t2 = parseInt(document.getElementById("TextBox2").value);
var t3 = parseInt(document.getElementById("TextBox3").value);
if((t1 >= 0 && t1 <= 100) && (t2 >= 0 && t2 <= 100) && (t3 >= 0 && t3 <= 100) && (t1 + t2 + t3 == 100))
{
return true;
}
window.alert("输入不合法");
return false;
}
//-->
</script>
......
<asp:TextBox id="TextBox1" runat="server" onkeypress="onlyNumber(this);"></asp:TextBox>
<asp:TextBox id="TextBox2" runat="server" onkeypress="onlyNumber(this);"></asp:TextBox>
<asp:TextBox id="TextBox3" runat="server" onkeypress="onlyNumber(this);"></asp:TextBox>
......
<asp:Button runat="server" id="btnSubmit" onclientclick="return valid();"></asp:Button>