checking form fields

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

    checking form fields

    Hi!
    I've got problem.
    Everything works fine when on line !1! is just single entry in if
    statement. Why after putting "&&" script doesn't work?

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <HTML>
    <HEAD>

    function checkAns( form )
    {
    var radiock;
    for(var i = 0; i < form.radio1.len gth; i++)
    {
    if( form.radio1[i].checked )
    {
    radiock = form.radio1[i].value;
    }
    }
    if( (form.forename. value == "") && (radiock == null) ) /* !1! */
    {
    alert("please answer all compulsory questions!");
    return false;
    }
    else
    {
    return true;
    }
    }
    </script>

    </HEAD>
    <BODY>
    <form name="myform" method="get" onsubmit="retur n checkAns(this); ">
    <P>
    Input your name(compulsory ): <input type="text" size="40"
    name="forename" >
    <br>
    <br>
    fav colour(compulso ry)?
    <br>
    <INPUT TYPE="radio" NAME="radio1" VALUE="red">red </P>
    <P>
    <INPUT TYPE="radio" NAME="radio1" VALUE="blue">bl ue</P>
    <P>
    <INPUT TYPE="radio" NAME="radio1" VALUE="green">g reen</P>
    <P>
    <INPUT TYPE="radio" NAME="radio1" VALUE="white">w hite</P>
    <P>
    marital status <INPUT TYPE="checkbox" name="marital" value=""></P>
    <P>
    any other comments <TEXTAREA NAME="comment" ROWS="5" COLS="15"
    WRAP="physical" ></TEXTAREA>
    </P>
    <P>
    <INPUT TYPE="submit" NAME="NAME" VALUE="submit" ID="Submit1">
    </P>
    </BODY>
    </HTML>

    any ideas?

    --
    Bremse
  • Phillip Parr

    #2
    Re: checking form fields

    hey, look down for my solution...

    "Bremse" <bremse{usun.to }@wp.pl> wrote in message
    news:l0udq052d7 aopj0n712mhkrkm jdchm1fek@4ax.c om...[color=blue]
    > Hi!
    > I've got problem.
    > Everything works fine when on line !1! is just single entry in if
    > statement. Why after putting "&&" script doesn't work?
    >
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    > <HTML>
    > <HEAD>
    >
    > function checkAns( form )
    > {
    > var radiock;
    > for(var i = 0; i < form.radio1.len gth; i++)
    > {
    > if( form.radio1[i].checked )
    > {
    > radiock = form.radio1[i].value;
    > }
    > }
    > if( (form.forename. value == "") && (radiock == null) ) /* !1! */[/color]

    Here you want to use || rather than && or the form can be submitted with an
    empty name if a colour has been chosen
    [color=blue]
    > {
    > alert("please answer all compulsory questions!");
    > return false;
    > }
    > else
    > {
    > return true;
    > }
    > }
    > </script>
    >
    > </HEAD>
    > <BODY>
    > <form name="myform" method="get" onsubmit="retur n checkAns(this); ">[/color]

    rather than sending 'this', send 'myform'.
    [color=blue]
    > <P>
    > Input your name(compulsory ): <input type="text" size="40"
    > name="forename" >
    > <br>
    > <br>
    > fav colour(compulso ry)?
    > <br>
    > <INPUT TYPE="radio" NAME="radio1" VALUE="red">red </P>
    > <P>
    > <INPUT TYPE="radio" NAME="radio1" VALUE="blue">bl ue</P>
    > <P>
    > <INPUT TYPE="radio" NAME="radio1" VALUE="green">g reen</P>
    > <P>
    > <INPUT TYPE="radio" NAME="radio1" VALUE="white">w hite</P>
    > <P>
    > marital status <INPUT TYPE="checkbox" name="marital" value=""></P>
    > <P>
    > any other comments <TEXTAREA NAME="comment" ROWS="5" COLS="15"
    > WRAP="physical" ></TEXTAREA>
    > </P>
    > <P>
    > <INPUT TYPE="submit" NAME="NAME" VALUE="submit" ID="Submit1">
    > </P>
    > </BODY>
    > </HTML>
    >
    > any ideas?
    >
    > --
    > Bremse[/color]


    Hope that solves your problem!

    Phil


    Comment

    • Fred Oz

      #3
      Re: checking form fields

      Bremse wrote:
      [...][color=blue]
      > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      > <HTML>
      > <HEAD>[/color]

      A title is required, may as well put it here...

      <title>Bremse 's Form</title>


      And for a script to work, you must tell the browser it's a script...


      <script type="text/javascript">
      [color=blue]
      >
      > function checkAns( form )
      > {
      > var radiock;[/color]

      var radiock = null;
      [color=blue]
      > for(var i = 0; i < form.radio1.len gth; i++)
      > {
      > if( form.radio1[i].checked )
      > {
      > radiock = form.radio1[i].value;
      > }
      > }
      > if( (form.forename. value == "") && (radiock == null) ) /* !1! */[/color]

      The extra brackets aren't needed

      if(form.forenam e.value == "" && radiock == null) /* !1! */

      The above will submit the form if either "forename" has a value or any
      radio button has been selected. It will only fail if both the value is
      "" *and* no radio button has been checked, it should be:

      if(form.forenam e.value == "" || radiock == null) /* !1! */

      If you are going to evaluate whether radiock is "null", then set it to
      null when you declare it. It is actually undefined, but if left
      unaltered, the expression will return true as you expect, but just in
      case...
      [color=blue]
      > {
      > alert("please answer all compulsory questions!");
      > return false;
      > }[/color]

      I the following is superfluous, if the function completes happily
      it will return true anyway.
      [color=blue]
      > else
      > {
      > return true;
      > }
      > }
      > </script>
      >
      > </HEAD>
      > <BODY>
      > <form name="myform" method="get" onsubmit="retur n checkAns(this); ">[/color]

      forms also must have an action, even if it's ""

      <input .... action="">
      [color=blue]
      > <P>
      > Input your name(compulsory ): <input type="text" size="40"
      > name="forename" >
      > <br>
      > <br>
      > fav colour(compulso ry)?
      > <br>
      > <INPUT TYPE="radio" NAME="radio1" VALUE="red">red </P>
      > <P>
      > <INPUT TYPE="radio" NAME="radio1" VALUE="blue">bl ue</P>
      > <P>
      > <INPUT TYPE="radio" NAME="radio1" VALUE="green">g reen</P>
      > <P>
      > <INPUT TYPE="radio" NAME="radio1" VALUE="white">w hite</P>
      > <P>
      > marital status <INPUT TYPE="checkbox" name="marital" value=""></P>
      > <P>
      > any other comments <TEXTAREA NAME="comment" ROWS="5" COLS="15"
      > WRAP="physical" ></TEXTAREA>
      > </P>
      > <P>
      > <INPUT TYPE="submit" NAME="NAME" VALUE="submit" ID="Submit1">
      > </P>[/color]

      It's good to let the browser know when the form is finished... but
      first add a reset button so it's easy for a user to clear the form.

      <input type="reset">
      </form>
      [color=blue]
      > </BODY>
      > </HTML>
      >[/color]

      Have fun...

      --
      Fred

      Comment

      • Fred Oz

        #4
        Re: checking form fields

        Fred Oz wrote:
        [...][color=blue]
        >
        > I the following is superfluous, if the function completes happily
        > it will return true anyway.
        >[/color]

        That should have been:

        The following else is superfluous...
        [color=blue][color=green]
        >> else
        >> {
        >> return true;
        >> }[/color][/color]
        [...][color=blue]
        >
        > forms also must have an action, even if it's ""
        >
        > <input .... action="">
        >[/color]

        and that should have been:

        <form .... action="">

        The joys of imperfection.

        --
        Fred

        Comment

        • Michael Winter

          #5
          Re: checking form fields

          On Fri, 26 Nov 2004 10:14:54 +0000 (UTC), Phillip Parr <no@no.com> wrote:
          [color=blue]
          > "Bremse" <bremse{usun.to }@wp.pl> wrote in message
          > news:l0udq052d7 aopj0n712mhkrkm jdchm1fek@4ax.c om...
          >[color=green]
          >> <form name="myform" method="get" onsubmit="retur n checkAns(this); ">[/color]
          >
          > rather than sending 'this', send 'myform'.[/color]

          The OP is correct here. When used in an intrinsic event, the this operator
          refers to the element. That particular code is equivalent to

          return checkAns(docume nt.forms['myform']);

          By the way, replacing either with

          return checkAns(myform );

          is unreliable and should definitely be avoided.

          [snip]

          Mike

          --
          Michael Winter
          Replace ".invalid" with ".uk" to reply by e-mail.

          Comment

          • Bremse

            #6
            Re: checking form fields

            (...)[color=blue]
            > Have fun...[/color]

            thx :-)

            --
            Bremse

            Comment

            • Bremse

              #7
              Re: checking form fields

              >>> <form name="myform" method="get" onsubmit="retur n checkAns(this); ">[color=blue][color=green]
              >>
              >> rather than sending 'this', send 'myform'.[/color]
              >
              >The OP is correct here. When used in an intrinsic event, the this operator
              >refers to the element. That particular code is equivalent to
              >
              > return checkAns(docume nt.forms['myform']);
              >
              >By the way, replacing either with
              >
              > return checkAns(myform );
              >
              >is unreliable and should definitely be avoided.[/color]

              Ok, I will change it :-)

              --
              cheers
              Bremse

              Comment

              • Michael Winter

                #8
                Re: checking form fields

                On Fri, 26 Nov 2004 20:53:09 +1000, Fred Oz <ozfred@iinet.n et.auau> wrote:

                [snip]
                [color=blue]
                > The extra brackets aren't needed
                >
                > if(form.forenam e.value == "" && radiock == null) /* !1! */[/color]

                True, but parentheses are good for readability, as much as determining
                precedence.

                [snip]
                [color=blue]
                > if(form.forenam e.value == "" || radiock == null) /* !1! */[/color]

                Which could be reduced further to

                if(!form.forena me.value || !radiock)
                [color=blue]
                > If you are going to evaluate whether radiock is "null", then set
                > it to null when you declare it. It is actually undefined, but if
                > left unaltered, the expression will return true as you expect, but
                > just in case...[/color]

                I would only expect such a comparison to fail with a broken ECMAScript
                implementation. The equality algorithm equates undefined and null. Only a
                strict equality (===) comparison should evaluate to false.

                [snip]
                [color=blue]
                > I the following is superfluous, if the function completes happily
                > it will return true anyway.[/color]

                A function that exits without any return statement, or with

                return;

                will return undefined, not true. However, only

                return false;

                will cancel an event so returning true or undefined is pretty much
                equivalent.

                [snip]

                Mike

                --
                Michael Winter
                Replace ".invalid" with ".uk" to reply by e-mail.

                Comment

                • Michael Winter

                  #9
                  Re: checking form fields

                  On Fri, 26 Nov 2004 11:29:41 +0000, Bremse <bremse{usun.to }@wp.pl> wrote:

                  [snip]
                  [color=blue]
                  > Ok, I will change it :-)[/color]

                  Ack!! What?

                  No, *don't* change it. You were right in the first place: use the this
                  operator!

                  Mike

                  --
                  Michael Winter
                  Replace ".invalid" with ".uk" to reply by e-mail.

                  Comment

                  • Phillip Parr

                    #10
                    Re: checking form fields

                    While you say the this operator was correct, you don't explain why his
                    script didn't work. Indeed, I didn't get it to work with 'this' either.

                    Phil

                    "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
                    news:opsh2ldbfx x13kvk@atlantis ...[color=blue]
                    > On Fri, 26 Nov 2004 11:29:41 +0000, Bremse <bremse{usun.to }@wp.pl> wrote:
                    >
                    > [snip]
                    >[color=green]
                    >> Ok, I will change it :-)[/color]
                    >
                    > Ack!! What?
                    >
                    > No, *don't* change it. You were right in the first place: use the this
                    > operator!
                    >
                    > Mike
                    >
                    > --
                    > Michael Winter
                    > Replace ".invalid" with ".uk" to reply by e-mail.[/color]


                    Comment

                    • Michael Winter

                      #11
                      Re: checking form fields

                      On Fri, 26 Nov 2004 12:03:47 +0000 (UTC), Phillip Parr <no@no.com> wrote:
                      [color=blue]
                      > While you say the this operator was correct, you don't explain why his
                      > script didn't work.[/color]

                      Because I assume that Fred's more comprehensive post was sufficient to
                      solve the OP's problem. I was merely correcting your misinformation.

                      Why should I post a solution if someone has already done so?

                      [snip]

                      Mike


                      Please don't top-post.

                      --
                      Michael Winter
                      Replace ".invalid" with ".uk" to reply by e-mail.

                      Comment

                      • Phillip Parr

                        #12
                        Re: checking form fields

                        sorry, that's my fault for not looking at the times!


                        "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
                        news:opsh2ntttd x13kvk@atlantis ...[color=blue]
                        > On Fri, 26 Nov 2004 12:03:47 +0000 (UTC), Phillip Parr <no@no.com> wrote:
                        >[color=green]
                        >> While you say the this operator was correct, you don't explain why his
                        >> script didn't work.[/color]
                        >
                        > Because I assume that Fred's more comprehensive post was sufficient to
                        > solve the OP's problem. I was merely correcting your misinformation.
                        >
                        > Why should I post a solution if someone has already done so?
                        >
                        > [snip]
                        >
                        > Mike
                        >
                        >
                        > Please don't top-post.
                        >
                        > --
                        > Michael Winter
                        > Replace ".invalid" with ".uk" to reply by e-mail.[/color]


                        Comment

                        Working...