SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2001
    Location
    San Francisco
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    validating and submitting

    Hello - I want to validate a form using Javascript and then submit it if all the fields are entered correctly. I was reading in one tutorial to make the submit button run an onsubmit="return validate()" function where it would check to see if all the fields were filled in and then return true or false.

    My question is, how do I make it submit if it returns true - indicating that the form is filled in? Thanks.

    Ian
    signature

  2. #2
    You want what? By when?? Milamber's Avatar
    Join Date
    Jan 2001
    Location
    California
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i think you put this in the form tag:

    onsubmit="validate();return true"

    however, if there's an error, have in that validate() function say "return false" and that should stop the submission.

    Not sure on this though.
    -Jeff Minard | jrm.cc - Battlefield 2 Stats

  3. #3
    SitePoint Zealot jonsteele's Avatar
    Join Date
    Jan 2001
    Location
    Toronto
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Put this in the <form> tag:
    Code:
    <form onsubmit="return validate(this)">
    Then you'd need a validate function to do your validating:
    Code:
    <script language="JavaScript">
    function validate(theForm){
      if(theForm.inputFieldName.value==""){
        alert("You forgot to fill in the inputFieldName field!");
        return false;
      }else{
        return true;
      }
    }
    </script>
    Anywhere you return false in your function, it will stop the form from submitting.

    Jon

  4. #4
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi nobodaddy,

    If interested, see the "Validating Forms" script/tutorial at my site (GrassBlade). It demonstrates and discusses validating all types of form elements for user non-usage. It also shows how you can submit with using 'onSubmit'

    Hi jon fancy meeting you here

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  5. #5
    SitePoint Zealot jonsteele's Avatar
    Join Date
    Jan 2001
    Location
    Toronto
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Vinny,

    Fancy that.

    Jon

    P.S. Ian: definitely check out Vinny's site...first place I learned javascipt.

  6. #6
    SitePoint Zealot
    Join Date
    Sep 2001
    Location
    San Francisco
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, it worked! Thanks. I ended up string a bunch of the fields together using || - seems to work great. Thanks again.

    Ian
    signature

  7. #7
    SitePoint Zealot
    Join Date
    Sep 2001
    Location
    San Francisco
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just bookmarked that site. Very cool! Thanks!
    signature

  8. #8
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Ian,

    Glad it worked out, and glad you bookmarked the site, but don't believe everything jon says -- he learned most of his javascript at the javascriptCity forums, where he's a moderator. I and my site just showed him how to open windows with blinds.

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still


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
  •