How to trigger ASP.NET client-side validations without submit?

empz picture empz · Jan 17, 2011 · Viewed 9.6k times · Source

I have a website in ASP.NET (WebForms, NOT MVC) which has a survey form divided in several slides. Each slide has a next button that, obviously does a transition (client-side, not post back or remote request) to the next slide.

In each slide I have several ASP.NET controls with their related validators. I want this validators to be triggered when I click the next button (or maybe when each input loses focus?).

I remembered ASP.NET doing client side validation on lost focus, but maybe I'm wrong... (I quit doing ASP.NET development about 3 years now, so I can't remember)

Thanks

UPDATE:

It would be better to make ASP.NET trigger each validator when the associated control lost focus. I remember ASP.NET doing this (or am I dreaming? =P)

Answer

Eric picture Eric · Jan 17, 2011

First you need to make sure all of your validators have target controls specified using the "TargetControlID" Attribute on the validators.

Then you can set up a validation group per page and specify the group name in your next button and on the controls themselves.

If you are using regular expression validators you can choose them from this website

To Validate Client Side If you are using custom validators you can create a client function and specify it on the custom validator using the ClientValidationFunction attribute and by setting EnableclientScript = "true" on the custom validator.

Just be sure your client function has sender and args parameters.