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

10,077

Solution 1

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.

Solution 2

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.

Solution 3

put all your client-side validators into the same validationgroup and with your 'next' button add the same validation group. When you click the button it will automatically trigger all the validators before it does the post-back.

as to manually triggering the validation... you might also be able to use ValidatorOnSubmit(). I remember doing this in another project but i'm having a hard time finding the code.

Share:
10,077
empz
Author by

empz

Updated on June 17, 2022

Comments

  • empz
    empz almost 2 years

    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)

  • empz
    empz over 13 years
    Problem is 'next' button is not really a button but a link that executes some javascript to make the transition to the next slide. Not post-back, nor remote request involded.
  • empz
    empz over 13 years
    Problem is 'next' button is not really a button but a link that executes some javascript to make the transition to the next slide. Not post-back, nor remote request involved.
  • John Smith
    John Smith over 13 years
    what about using the onchange() javascript method? or onblur()?
  • John Smith
    John Smith over 13 years
    you would use those methods on the text box themselves.