check form data before sending to a server

Discussion in 'Javascript' started by Dknight, Nov 10, 2005.

  1. Dknight

    Dknight Guest

    Hi, all!
    I have a problem, how can I check a form data before sending it to
    server. I have the example below, but first data is sent to server, but
    after JavaScript checks the data :(

    I need that JavaSctipt check data and after send it to the server, how
    can do that?

    Example:
    ....
    <script language="javascript">
    function check()
    {
    if(document.forms[0].name1.value == "")
    {
    alert("enter name");
    return false;
    }
    else return true;
    }
    </script>
    ....
    <form action="someapp.php" onSubmit="return check()">
    <input type="text" name="name1">
    <input type="submit">
    </form>
    ....

    Thanks in advance!
     
    Dknight, Nov 10, 2005
    #1
    1. Advertising

  2. Dknight

    McKirahan Guest

    "Dknight" <> wrote in message
    news:...
    > Hi, all!
    > I have a problem, how can I check a form data before sending it to
    > server. I have the example below, but first data is sent to server, but
    > after JavaScript checks the data :(
    >
    > I need that JavaSctipt check data and after send it to the server, how
    > can do that?


    [snip]

    What makes you think that the data is sent to the server first?

    "onSubmit()" is invoked when you hit the "submit" button;
    only if "check()" returns "true" is "action=" performed.

    Your code could be simplified somewhat:

    <script type="text/javascript">
    function check(form) {
    if (form.name1.value == "") {
    alert("enter name");
    return false;
    }
    return true;
    }
    </script>
    ....
    <form action="someapp.php" onSubmit="return check(this)">
    <input type="text" name="name1">
    <input type="submit">
    </form>
    ....
     
    McKirahan, Nov 10, 2005
    #2
    1. Advertising

  3. Dknight

    RobG Guest

    McKirahan wrote:
    > "Dknight" <> wrote in message
    > news:...
    >
    >>Hi, all!
    >>I have a problem, how can I check a form data before sending it to
    >>server. I have the example below, but first data is sent to server, but
    >>after JavaScript checks the data :(
    >>
    >>I need that JavaSctipt check data and after send it to the server, how
    >>can do that?

    >
    >
    > [snip]
    >
    > What makes you think that the data is sent to the server first?
    >
    > "onSubmit()" is invoked when you hit the "submit" button;
    > only if "check()" returns "true" is "action=" performed.
    >
    > Your code could be simplified somewhat:
    >
    > <script type="text/javascript">
    > function check(form) {
    > if (form.name1.value == "") {


    Or:

    if ( !form.name1.value ) {


    > alert("enter name");
    > return false;
    > }
    > return true;


    There is no need to return true, just don't return false.

    [...]


    --
    Rob
     
    RobG, Nov 10, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Assimalyst

    Check form before unload for changes

    Assimalyst, Aug 10, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    5,427
    Assimalyst
    Aug 11, 2005
  2. Replies:
    1
    Views:
    427
    Beauregard T. Shagnasty
    Oct 4, 2006
  3. Bjorn Sagbakken

    Pop-up form sending data to the calling form?

    Bjorn Sagbakken, Jun 30, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    506
    Bjorn Sagbakken
    Jun 30, 2007
  4. Attila.Iskander
    Replies:
    14
    Views:
    660
    Adrienne Boswell
    Sep 22, 2011
  5. Raakeli
    Replies:
    1
    Views:
    134
    Raakeli
    Mar 21, 2005
Loading...

Share This Page