Executing server validators first before OnClientClick Javascript confirm/alert

I got to answer a simple question over community forums.

Consider this: Suppose you are developing a webpage with few input controls and a submit button. You have placed some server validator controls like RequiredFieldValidator to validate the inputs entered by the user. Once user fill-in all the details and try to submit the page via button click you want to alert/confirm the submission like "Are you sure to modify above details?". You will consider to use javascript on click of the button.

Everything seems simple and you are almost done. BUT, when you run the page; you will see that Javascript alert/confirm box is executing first before server validators try to validate the input controls! Well, this is expected behaviour. BUT, this is not you want. Then?

The simple answer is: Call Page_ClientValidate() in javascript where you are alerting the submission. Below is the javascript example:

    <script type="text/javascript" language="javascript">
        function ValidateAllValidationGroups() {
            if (Page_ClientValidate()) {
                return confirm("Are you sure to modify above details?");
            }
        }
    </script>

Page_ClientValidate() function tests for all server validators and return bool value depends on whether the page meets defined validation criteria or not. In above example, confirm alert will only popup up if Page_ClientValidate() returns true (if all validations satisfy). You can also specify ValidationGroup inside this function as Page_ClientValidate('ValidationGroup1') to only validate a specific group of validation in your page.

        function ValidateSpecificValidationGroup() {
            if (Page_ClientValidate('ValidationGroup1')) {
                return confirm("Are you sure to modify above details?");
            }
        }

I have attached a sample example with this post here demonstrating both above cases. Hope it helps./.

Comments

# re: Executing server validators first before OnClientClick Javascript confirm/alert

Monday, February 28, 2011 9:38 PM by kashmirindotnet

Thanks a lot.

# re: Executing server validators first before OnClientClick Javascript confirm/alert

Wednesday, March 02, 2011 6:39 AM by Mathew

Excellent stuff.

# re: Executing server validators first before OnClientClick Javascript confirm/alert

Thursday, May 26, 2011 12:25 AM by Hieu

Great code, that's exactly what I need

# re: Executing server validators first before OnClientClick Javascript confirm/alert

Thursday, July 21, 2011 2:56 AM by Amjad

You are my Hero Long Live Kaushal

Thanks A Million

# re: Executing server validators first before OnClientClick Javascript confirm/alert

Tuesday, September 06, 2011 9:26 AM by seenuvasan

This helped me lot !!!!. Thank you very much for your great stuff

# re: Executing server validators first before OnClientClick Javascript confirm/alert

Monday, September 12, 2011 3:59 PM by UmairZubairy

This works .. thank you soo much ..  

# re: Executing server validators first before OnClientClick Javascript confirm/alert

Friday, January 13, 2012 7:53 AM by Vinicius Lopes de Almeida

You save my life!!!!

thanks!!!!

# re: Executing server validators first before OnClientClick Javascript confirm/alert

Tuesday, March 27, 2012 10:17 PM by Jeff

This solution is very useful, thank you

# re: Executing server validators first before OnClientClick Javascript confirm/alert

Monday, April 09, 2012 1:29 AM by DN

thx!!!1

# re: Executing server validators first before OnClientClick Javascript confirm/alert

Friday, May 18, 2012 3:18 AM by vspEXGfYeOvVjUo

Thanks-a-mundo for the blog article.Really thank you! Really Great.

# re: Executing server validators first before OnClientClick Javascript confirm/alert

Friday, June 29, 2012 7:45 PM by Seo

I loved your article.Really thank you! Really Cool.

# re: Executing server validators first before OnClientClick Javascript confirm/alert

Friday, February 01, 2013 9:55 AM by BiEmUvCqRxBIkuXFiNK

A round of applause for your blog. Keep writing.

Leave a Comment

(required) 
(required) 
(optional)
(required)