Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 490
empz Avatar asked Jan 17 '11 18:01

empz


2 Answers

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.

like image 88
Eric Avatar answered Sep 24 '22 07:09

Eric


It looks like there's a supplied JavaScript function called Page_ClientValidate which should be callable to check the validation manually from JavaScript. I haven't used it, though, so YMMV.

like image 36
David Avatar answered Sep 22 '22 07:09

David