SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    Temecula, CA
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    return Form Validation using addEventListener

    This is all very well and good:
    Code:
    <form  action = 'ContactFormProcess.php'  method = 'post' id = "InfoForm"  onSubmit= 'return VerifyForm()'>
    And so is this:
    Code:
    infoForm.addEventListener('submit', VerifyForm, true);
    But how do I incorporate the 'return' into the addEventListener syntax?

  2. #2
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think you do.
    With event listeners you have to use the event object to stop the default action.
    Code:
    if (e.preventDefault) {
      e.preventDefault(); // That's the W3C way
    } else {
      e.returnValue = false; // for IE
    }

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    Temecula, CA
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Where do I put this code? I'm thinking within the event handler? With the 'return' method one uses return false, right? So, is this the equivalent to that? Is the event object present by default? I'm new to the use of this object.

  4. #4
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    Temecula, CA
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've found that if I use an 'e' as the sole parameter of the event handling function the code works fine within.

  5. #5
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, but be careful, IE doesn't pass the event object, e, as a parameter. A cross-browser way to get e is
    Code:
    function eventHandler(e) {
      e = e || window.event;
      // Your function code here
    }
    This method sets e to be the passed in event object or, if it hasn't been passed in (i.e. we're using IE), sets e to be window.event

    You can then do whatever you want your listener to do including preventing the default action.

  6. #6
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    Temecula, CA
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks immensely! I don't test enough in IE. From now on, I will keep it open and parallel test continuously.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •