Asp.net validators - call validation by javascript

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();" />

About Author

Kiran Beladiya

Co-Founder

Kiran Beladiya is the co-founder of The One Technologies. He plays a key role in managing the entire project lifecycle, from discussing ideas with clients to overseeing successful releases. Deeply passionate about technology and creativity, he is also an avid writer who continues to nurture and refine his writing skills despite a demanding schedule. Through his work and writing, Kiran Beladiya shares practical insights drawn from real-world experience.

Certified By