Form validation before onClientClick

Last post 06-05-2008 3:47 AM by ramblor. 8 replies.

Sort Posts:

  • Form validation before onClientClick

    06-04-2008, 6:46 AM
    • Member
      8 point Member
    • EF1978
    • Member since 05-01-2008, 10:58 AM
    • Posts 27

    Can anyone tell me how to make my page run form valdation before the onClient click??

    My page only executes to validation control after I click 'Yes' in the message box.

    Or, what are the alternatives to the onClientClick event?

  • Re: Form validation before onClientClick

    06-04-2008, 7:49 AM
    • Star
      8,559 point Star
    • siva_sm
    • Member since 12-20-2007, 11:03 AM
    • Posts 1,256
    Call the client-side JS function Page_ClientValidate(). This will validate the controls that have associated validator controls. This function will be available only if your page uses at least one ASP.NET validator control.
    Mark replies as answers if they helped you solve the problem.
  • Re: Form validation before onClientClick

    06-04-2008, 7:54 AM
    • Contributor
      3,068 point Contributor
    • blurearc
    • Member since 04-25-2008, 11:15 AM
    • Mumbai India
    • Posts 537

    if you are using validator controls then above given solution is good.. or.. you have to tell us what you want to do...

    as form validations happens only when form is posted.. or if oyu have a totally diff function of validation on form then call that function in your onClientClick handler's first line..

    "Mark as answered if you feel this helps you"
    "Curiosity is Bliss"
    Regards,

    Ravi Kant Srivastava
    (Sr. Consultant I)
    Neudesic
    Hyd'bad
    INDIA
  • Re: Form validation before onClientClick

    06-04-2008, 8:16 AM
    • Member
      8 point Member
    • EF1978
    • Member since 05-01-2008, 10:58 AM
    • Posts 27

    my page has a form that has several required fields which are then submitted to a sqlserver database. but before the data is inserted I have (up until now) a message box that says 'Are you sure you want to submit?'

    so the validators are simply required field validators.

  • Re: Form validation before onClientClick

    06-04-2008, 8:22 AM
    • Contributor
      3,068 point Contributor
    • blurearc
    • Member since 04-25-2008, 11:15 AM
    • Mumbai India
    • Posts 537

    then set causevalidation false

    and use this

    Page_ClientValidate() on the very first line of your onClientClick's event handler in javascript

    "Mark as answered if you feel this helps you"
    "Curiosity is Bliss"
    Regards,

    Ravi Kant Srivastava
    (Sr. Consultant I)
    Neudesic
    Hyd'bad
    INDIA
  • Re: Form validation before onClientClick

    06-04-2008, 8:27 AM
    Answer
    • Contributor
      6,676 point Contributor
    • ramblor
    • Member since 03-13-2008, 6:03 AM
    • Posts 1,013
    If I've understood correctly, you want to validate the page client-side before the user is asked to confirm that they want to submit the form? If that's right then I think you can use the Page_ClientValidate() function as suggested by siva_sm and the OnClientClick of your button, something like:
    function ValidateMe()
    {
    var ok = Page_ClientValidate();
    if(ok)
    return confirm("Are you sure?");
    else
    return false;
    }
    <asp:Button ID="Button1" runat="server" Text="Submit" OnClientClick="if (!ValidateMe()) return false;" />
    "Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
  • Re: Form validation before onClientClick

    06-04-2008, 10:29 AM
    • Member
      8 point Member
    • EF1978
    • Member since 05-01-2008, 10:58 AM
    • Posts 27

    thanks guys, this now works great.

  • Re: Form validation before onClientClick

    06-04-2008, 10:57 AM
    • Member
      8 point Member
    • EF1978
    • Member since 05-01-2008, 10:58 AM
    • Posts 27

    I have just stumbled across a small problem. After the form validation is done, my drop down boxes will not perform a postback.

    Any ideas??

  • Re: Form validation before onClientClick

    06-05-2008, 3:47 AM
    • Contributor
      6,676 point Contributor
    • ramblor
    • Member since 03-13-2008, 6:03 AM
    • Posts 1,013

    I'm not sure if there is a better way to fix the problem, but I managed to replicate it and fix it. If you update the js function to this it should work:

    function ValidateMe()
    {
        var ok = Page_ClientValidate();
        if(ok)
        {
            return confirm("Are you sure?");
        }
        else
        {
            Page_BlockSubmit = false;
            return false;
        }
    }

    The Page_BlockSubmit variable is set to true when validation fails and stops the page being posted back on a subsequent try, even from a dropdown which isn't causing validation. This just happens the first time - change the value in the dropdown again and the page will postback (Page_BlockSubmit is reset to false the first time you select something in your dropdown so subsequent postbacks are fine). I hope that helps.

    "Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
Page 1 of 1 (9 items)