如何在ASP.NET C#中调用RangeValidator错误的javascript代码?

时间:2015-02-26 21:03:20

标签: javascript c# asp.net

我在文本字段上使用RangeValidator,并且在更改值并且不在指定范围内时,错误文本会正确设置。但是,我想运行一些Javascript,但我似乎无法触发javascript。

如何在错误时运行javascript?

我尝试使用onerror,但这似乎不起作用:

<asp:RangeValidator ID="maxUsersRangeValidator" runat="server" ControlToValidate="maxNumUsersTextBox" ErrorMessage="Error: max users must be between 100 and 1,000 users per batch" ForeColor="Red" MaximumValue="1000" MinimumValue="100" onerror="javascript:closeloading()" Type="Integer"></asp:RangeValidator>

1 个答案:

答案 0 :(得分:5)

您可以使用CustomValidator:

 <asp:CustomValidator ID="CustomValidator1" runat="server" EnableClientScript="true" ForeColor="Red" ErrorMessage="Error: max users must be between 100 and 1,000 users per batch" ClientValidationFunction="validateRange" ControlToValidate="maxNumUsersTextBox"></asp:CustomValidator>

然后,使用Javascript验证范围:

function validateRange(sender, args){
var cant = document.getElementById("maxNumUsersTextBox");
if(cant>=100 && cant<=1000){
   args.IsValid = true;
}else{
closeLoading();
   args.IsValid = false;
}
}