Before Form Submit?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brett Baisley

    Before Form Submit?

    Hello

    I am using the following line of code at the very end of my html file to
    run a javascript function before a form is submitted.

    document.forms[0].onsubmit = chkUsername;

    It works for one method, but what do I do when I want to call 2 methods? I
    tried this:

    document.forms[0].onsubmit = chkUsername;
    document.forms[0].onsubmit = chkUsername2;

    but the last one is the only one that is run. It seems overwrite the first
    one.

    Does anyone have any suggestions?

    Thanks


  • Michael Winter

    #2
    Re: Before Form Submit?

    On Tue, 24 Feb 2004 23:52:31 GMT, Brett Baisley
    <baisley@hotmai l.com.REMOVETHI S> wrote:
    [color=blue]
    > I am using the following line of code at the very end of my html file to
    > run a javascript function before a form is submitted.
    >
    > document.forms[0].onsubmit = chkUsername;
    >
    > It works for one method, but what do I do when I want to call 2 methods?
    > I tried this:
    >
    > document.forms[0].onsubmit = chkUsername;
    > document.forms[0].onsubmit = chkUsername2;
    >
    > but the last one is the only one that is run. It seems overwrite the
    > first one.[/color]

    It does.
    [color=blue]
    > Does anyone have any suggestions?[/color]

    document.forms[ 0 ].onsubmit = function() {
    chkUsername();
    chkUsername2();
    }

    or

    document.forms[ 0 ].onsubmit = validateForm;

    where validateForm is a function that calls chkUsername() followed by
    chkUsername2().

    Hope that helps,
    Mike

    --
    Michael Winter
    M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

    Comment

    • Brett Baisley

      #3
      Re: Before Form Submit?

      Ok, thanks. But if either of the functions the main one class return false,
      will it stop?


      "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
      news:opr3wgjhgs 5vklcq@news-text.blueyonder .co.uk...[color=blue]
      > On Tue, 24 Feb 2004 23:52:31 GMT, Brett Baisley
      > <baisley@hotmai l.com.REMOVETHI S> wrote:
      >[color=green]
      > > I am using the following line of code at the very end of my html file to
      > > run a javascript function before a form is submitted.
      > >
      > > document.forms[0].onsubmit = chkUsername;
      > >
      > > It works for one method, but what do I do when I want to call 2 methods?
      > > I tried this:
      > >
      > > document.forms[0].onsubmit = chkUsername;
      > > document.forms[0].onsubmit = chkUsername2;
      > >
      > > but the last one is the only one that is run. It seems overwrite the
      > > first one.[/color]
      >
      > It does.
      >[color=green]
      > > Does anyone have any suggestions?[/color]
      >
      > document.forms[ 0 ].onsubmit = function() {
      > chkUsername();
      > chkUsername2();
      > }
      >
      > or
      >
      > document.forms[ 0 ].onsubmit = validateForm;
      >
      > where validateForm is a function that calls chkUsername() followed by
      > chkUsername2().
      >
      > Hope that helps,
      > Mike
      >
      > --
      > Michael Winter
      > M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)[/color]


      Comment

      • Michael Winter

        #4
        Re: Before Form Submit?

        On Wed, 25 Feb 2004 22:24:16 GMT, Brett Baisley
        <baisley@hotmai l.com.REMOVETHI S> wrote:

        [snipped top-post]
        [color=blue]
        > Ok, thanks. But if either of the functions the main one class return
        > false, will it stop?[/color]

        Yes. Take this modified version of an example in my last post:

        document.forms[ 0 ].onsubmit = function() {
        if( !chkUsername() ) return false;
        return chkUsername2();
        }

        If chkUsername() returns false, the event handler will exit, return false,
        and cancel the submission. If it returns true, execution will continue
        with the call the chkUsername2(). Whatever the return value from that
        function, it will be return it as the function exits. If the value
        evaluates to false, the submission will be cancelled.

        Mike

        --
        Michael Winter
        M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

        Comment

        • Grant Wagner

          #5
          Re: Before Form Submit?

          Michael Winter wrote:
          [color=blue]
          > On Wed, 25 Feb 2004 22:24:16 GMT, Brett Baisley
          > <baisley@hotmai l.com.REMOVETHI S> wrote:
          >
          > [snipped top-post]
          >[color=green]
          > > Ok, thanks. But if either of the functions the main one class return
          > > false, will it stop?[/color]
          >
          > Yes. Take this modified version of an example in my last post:
          >
          > document.forms[ 0 ].onsubmit = function() {
          > if( !chkUsername() ) return false;
          > return chkUsername2();
          > }
          >[/color]

          Unless I'm missing something, what you wrote above could be replaced with:

          document.forms[ 0 ].onsubmit = function() {
          return chkUsername() && chkUsername2();
          }

          --
          | Grant Wagner <gwagner@agrico reunited.com>

          * Client-side Javascript and Netscape 4 DOM Reference available at:
          *


          * Internet Explorer DOM Reference available at:
          *
          Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


          * Netscape 6/7 DOM Reference available at:
          * http://www.mozilla.org/docs/dom/domref/
          * Tips for upgrading JavaScript for Netscape 7 / Mozilla
          * http://www.mozilla.org/docs/web-deve...upgrade_2.html


          Comment

          • Michael Winter

            #6
            Re: Before Form Submit?

            On Fri, 27 Feb 2004 17:38:17 GMT, Grant Wagner
            <gwagner@agrico reunited.com> wrote:
            [color=blue]
            > Michael Winter wrote:[/color]

            [snip]
            [color=blue][color=green]
            >> document.forms[ 0 ].onsubmit = function() {
            >> if( !chkUsername() ) return false;
            >> return chkUsername2();
            >> }[/color]
            >
            > Unless I'm missing something, [...][/color]

            Nope.
            [color=blue]
            > what you wrote above could be replaced with:[/color]

            I knew there had to be a way to make it smaller (mine seemed unnecessarily
            bulky), and your suggestion is that way.
            [color=blue]
            > document.forms[ 0 ].onsubmit = function() {
            > return chkUsername() && chkUsername2();
            > }[/color]

            Thank you. :)

            Mike

            --
            Michael Winter
            M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

            Comment

            Working...