ASP.NET Fire Validation
Manually firing a single validator using client-side script
In ASP.Net the use of validators is common place, including the <asp:CustomValidator>
.
If a validator is linked to a control, then that validator is fired when the control is changed... but what about when multiple controls are used on a single custom validator? How do you make sure the validator shows the correct value when you change any of those controls?
One answer is to make all those controls call an extra function... which itself then uses the inbuilt ValidatorValidate()
provided by ASP.Net.
Markup...
<asp:TextBox runat="server" id="txtFirst" onchange="textboxChanged();" />
<asp:TextBox runat="server" id="txtSecond" onchange="textboxChanged();" />
<asp:CustomValidator runat="server" ID="cusVal" ErrorMessage="Oops"
ClientValidationFunction="doCustomVal" />
Javascript...
function textboxChanged() {
ValidatorValidate(document.getElementById("<%=cusVal.ClientID%>"));
}
function doCustomVal(src, args) {
args.IsValid = (
document.getElementById("<%=txtFirst.ClientID%>").value != "" &&
document.getElementById("<%=txtSecond.ClientID%>").value != "");
}
Added 04/06/2018 15:58