SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Nov 2003
    Location
    Barcelona
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Ways to validate a form

    I want to validate a form with a function, to send the data to the server (and insert into the database) and then go to the next page. I want to do it with a hyperlink, and now I know four possibilities to validate a form. Which is the best and why?


    1.
    <form action="next_page.asp" onSubmit="return checkMail(this.form)" method="post">

    <input type="submit" value="Submit">
    </form>



    2.
    <form>

    <a href="next_page.asp" onClick="return checkMail(this.form)">To send data</a>
    </form>



    3.
    <script language="JavaScript">
    <!--
    function checkMail(form)
    {
    for (var = i; i < form.elements.length; i++ {
    if (form.elements[i].value == "") {
    alert("Fill out ALL fields.")
    return
    }
    }
    form.submit()
    return
    // -->
    </script>
    }


    <form>
    <a href="next_page.asp" onClick="return checkMail(this.form)">To send data</a>
    </form>



    4.
    <form action="next_page.asp" method="post">
    </form>

    <a href="javascript:document.forms[0].submit()">To send data</a>

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    imo, 1 has it, hands down. 2 makes no sense:

    <a href="next_page.asp" onClick="return checkMail(this.form)">To send data</a>

    ...as the Link object involved (represents the <a>) has no .form property, even though you stuck it inside <form> tags. Still not a form element. Could use an absolute reference (document.form_name) of course. Even so, if the validator returns true, loading the new url is not the same as submitting the form, obviously.

    3: here you're submitting and loading a new page. Kind of indiscriminate validation, as well.

    4: Totally sucks. Gave up, submit it, I don't care.

    Use the onsubmit handler, that's what it's there for. Alternatively, you could use the Submit.onclick handler, same result. And, btw:

    onSubmit="return checkMail(this)"

    Inside the <form> tag, this references the Form object directly.
    ::: certified wild guess :::

  3. #3
    SitePoint Member
    Join Date
    Nov 2003
    Location
    Barcelona
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by adios
    Use the onsubmit handler, that's what it's there for. Alternatively, you could use the Submit.onclick handler, same result.
    According to your comment above, how can I use exactly the 'Submit.onlick' handler in the first case? Maybe thus?:
    (I want to use a link)

    <form action="next_page.asp" onSubmit="return checkMail(this.form)" method="post">

    <a href="" onClick="submit">To send data</a>
    </form>

    Thanks,
    Cesar

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how can I use exactly the 'Submit.onlick' handler....
    You must be over 18 to use that handler.

    But seriously, folks...the Submit in Submit.onclick refers to an <input type="submit/image"...> - not what you wanted. You can submit any form, from anywhere, by calling ref_to_Form.submit() - but it will not call the Form's onsubmit handler, as with a button/image submit. The idea seems to be: you're obviously scripting this anyway so, call it yourself!
    Code:
    <form action="next_page.asp" method="post">
    
    <a href="#null" onclick="f=document.forms[0];if(checkMail(f))f.submit();return false">To send data</a> 
    </form>
    This assumes the first form ([0]) in the page.
    ::: certified wild guess :::

  5. #5
    SitePoint Member
    Join Date
    Nov 2003
    Location
    Barcelona
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, thank you


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
  •