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 button’s OnClientClick function, which breaks the client side code which calls validators to validate the form fields.

The quick work around to this problem is to call following function right after your custom function 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 validation group with name “registration”, you can use overload of Page_ClientValidate() in which you can pass validationgroup name.

function callValidators(groupName)
{
	Page_ClientValidate(groupName);
	if(Page_IsValid)
		return true;
	else
		return false;
}

here is how we can call it from asp.net button:

<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="customFunction();return callValidators();" />

Loading