Asp.net validators - call validation by javascript
Often we need to use client-side javascript code to perform some logic on button click. We usually call the javascript function from the button’s OnClientClick function, which breaks the client-side code and calls validators to validate the form fields.
The quick workaround to this problem is to call the following function right after your custom function is written in OnClientClick.
function callValidators()
{
Page_ClientValidate();
if(Page_IsValid)
return true;
else
return false;
}
Javascript validation in case of ValidationGroup is used
If you are using a validation group with the name “registration”, you can use the overload of Page_ClientValidate() in which you can pass the validationgroup name.
function callValidators(groupName)
{
Page_ClientValidate(groupName);
if(Page_IsValid)
return true;
else
return false;
}
here is how we can call it from the asp.net button:
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="customFunction();return callValidators();" />