Reputation: 4005
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>
Upvotes: 0
Views: 1151
Reputation: 137
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;
}
}
Upvotes: 5