简体   繁体   中英

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. However, I want to run some Javascript as well but I can't seem to get the javascript to trigger.

How can I make javascript run on error?

I tried using onerror but that didn't seem to work:

<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:

 <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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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