简体   繁体   English

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

[英]How do I call javascript code on RangeValidator error in ASP.NET C#?

I use a RangeValidator on a text field and the error text gets set properly when the value is changed and it's not within the range specified. 我在文本字段上使用RangeValidator,并且在更改值且其不在指定范围内时,错误文本会正确设置。 However, I want to run some Javascript as well but I can't seem to get the javascript to trigger. 但是,我也想运行一些Javascript,但似乎无法触发Javascript。

How can I make javascript run on error? 如何使javascript错误运行?

I tried using onerror but that didn't seem to work: 我尝试使用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>

You can use CustomValidator: 您可以使用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>

Then, validate the range with Javascript: 然后,使用Javascript验证范围:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM