*Please Help*

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

    *Please Help*

    Hi there!
    Newbie here... :-)
    I'm trying to validate a form but can't seem to get it to work... Here's a
    copy of the code:
    The user has to select a color for every select box. How is this done???
    Many thanks
    Ann
    Ottawa Canada
    *************** *
    <HTML>
    <HEAD>
    <Title>My color test</Title>
    <script>

    function displayColors(f orm)
    {
    var colorArray = new Array(3);
    var box1Index, box2Index, box3Index;
    var myString='';

    box1Index=docum ent.form1.color 1.selectedIndex ;
    box2Index=docum ent.form1.color 2.selectedIndex ;
    box3Index=docum ent.form1.color 3.selectedIndex ;

    colorArray[0]=document.form1 .color1[box1Index].value;
    colorArray[1]=document.form1 .color2[box2Index].value;
    colorArray[2]=document.form1 .color3[box3Index].value


    for (i=0; i<3; i++)
    {
    switch(i)
    {
    case 0:
    myString="first ";
    break;

    case 1:
    myString="secon d";
    break;

    case 2:
    myString="third ";
    break;

    default:
    myString=""
    }
    switch (colorArray[i])
    {
    case "1":
    document.write( '<FONT COLOR="blue">') ;
    document.write( "you selected blue as your " + myString +" color<br>")
    document.write( '</FONT>')
    break;

    case "2":
    document.write( '<FONT COLOR="red">');
    document.write( "you selected red as your " + myString +" color<br>")
    document.write( '</FONT>')
    break;

    case "3":
    document.write( '<FONT COLOR="black">' );
    document.write( "you selected black as your " + myString +" first
    color<br>")
    document.write( '</FONT>')
    break;

    default:
    document.write( "no color<BR>");
    }//end switch
    }//end for
    }//end function

    function validateOnSubmi t(form)
    {
    /*??????*/
    }

    </script>
    </HEAD>
    <BODY>
    <form name=form1 onsubmit="valid ateOnSubmit(thi s.form)">
    Pick color 1:
    <SELECT id=color1 name=color1 style="WIDTH: 180px">
    <OPTION value = "0">Select a color
    <OPTION value = "1" name="blue">Blu e
    <option value = "2" name="red">Red
    <option value = "3" name="black">Bl ack
    </OPTION></SELECT>
    <br><br>
    Pick color 2:
    <SELECT id=color2 style="WIDTH: 180px" name=color2>
    <OPTION value = "0"> Select a color
    <OPTION value = "1" name="blue">Blu e
    <option value = "2" name="red">Red
    <option value = "3" name="black">Bl ack
    </OPTION></SELECT>
    <br><br>
    Pick color 3:
    <SELECT id=color3 style="width: 180px" name=color3>
    <OPTION value = "0"> Select a color
    <OPTION value = "1" name="blue">Blu e
    <option value = "2" name="red">Red
    <option value = "3" name="black">Bl ack
    </OPTION></SELECT>
    <br>
    <input TYPE=BUTTON NAME="cmdCalc" VALUE="GO"
    onClick=display Colors(this.for m)>
    </BODY>
    </HTML>



  • Robert

    #2
    Re: *Please Help*

    "Ann Duguay" <annduguayNOSPA M@yahoo.ca> wrote in message news:<Zy7Sc.202 $Ah.149@news04. bloor.is.net.ca ble.rogers.com> ...[color=blue]
    > Hi there!
    > Newbie here... :-)
    > I'm trying to validate a form but can't seem to get it to work... Here's a
    > copy of the code:
    > The user has to select a color for every select box. How is this done???
    > Many thanks
    > Ann
    > Ottawa Canada
    > *************** *[/color]

    Is this your first attempt to learn programming? Is this a student
    project?

    I ask this because most progammers would just let the data in the DOM
    and because selecting a color per select box seems contrived.

    Document.write can only be used to insert html into the file when the
    document is loading. In your case the file has been loaded, you need
    to try somehting like innerHTML. To keep it simple, you may just want
    to put up an alert box.

    Robert

    Comment

    • Ann Duguay

      #3
      Re: *Please Help*

      Hi Robert!
      Nope.. not a school project :-) I'm getting a little too old for school.
      I'm trying to help out a friend that's doing something in HTML but doesn't
      have any javascript knowledge. After looking at some code, I thought it
      would be easy but I'm having a difficult time.

      Her project is a little more complex as it has 55 questions with select
      boxes for each and every questions needs to be answered. Upon completion, a
      score is calculated based on the answers the user gives and a color report
      is printed to the screen (color also is score dependent).

      Hence, my small piece of code. I figured I'd start small but I'm stuck with
      the validation. All I want to do is to alert the user when the user hasn't
      selected anything (or the value is 0).

      Thx for taking the time to look at my code.
      Ann
      "Robert" <rccharles@my-deja.com> wrote in message
      news:c6bb75ff.0 408101916.25a4c f10@posting.goo gle.com...[color=blue]
      > "Ann Duguay" <annduguayNOSPA M@yahoo.ca> wrote in message[/color]
      news:<Zy7Sc.202 $Ah.149@news04. bloor.is.net.ca ble.rogers.com> ...[color=blue][color=green]
      > > Hi there!
      > > Newbie here... :-)
      > > I'm trying to validate a form but can't seem to get it to work...[/color][/color]
      Here's a[color=blue][color=green]
      > > copy of the code:
      > > The user has to select a color for every select box. How is this[/color][/color]
      done???[color=blue][color=green]
      > > Many thanks
      > > Ann
      > > Ottawa Canada
      > > *************** *[/color]
      >
      > Is this your first attempt to learn programming? Is this a student
      > project?
      >
      > I ask this because most progammers would just let the data in the DOM
      > and because selecting a color per select box seems contrived.
      >
      > Document.write can only be used to insert html into the file when the
      > document is loading. In your case the file has been loaded, you need
      > to try somehting like innerHTML. To keep it simple, you may just want
      > to put up an alert box.
      >
      > Robert[/color]


      Comment

      • kaeli

        #4
        Re: *Please Help*

        In article <__lSc.373267$r CA1.343910@news 01.bloor.is.net .cable.rogers.c om>,
        annduguayNOSPAM @yahoo.ca enlightened us with...[color=blue]
        > Hi Robert!
        > Nope.. not a school project :-) I'm getting a little too old for school.
        > I'm trying to help out a friend that's doing something in HTML but doesn't
        > have any javascript knowledge. After looking at some code, I thought it
        > would be easy but I'm having a difficult time.
        >
        > Her project is a little more complex as it has 55 questions with select
        > boxes for each and every questions needs to be answered. Upon completion, a
        > score is calculated based on the answers the user gives and a color report
        > is printed to the screen (color also is score dependent).
        >
        > Hence, my small piece of code. I figured I'd start small but I'm stuck with
        > the validation. All I want to do is to alert the user when the user hasn't
        > selected anything (or the value is 0).
        >[/color]

        I'll give you some hints.
        [color=blue]
        > box1Index=docum ent.form1.color 1.selectedIndex ;[/color]
        That is not as cross-browser as
        box1Index=docum ent.forms["form1"].elements["color1"].selectedIndex;

        Now, your function has the form passed in, so you can do
        function displayColors(f rm)
        Note that I changed "form" to "frm" because I have this thing about having a
        variable with the same name as an already defined object ("form" is already
        an object in JS).

        So, you passed in "frm", so
        box1Index=frm.e lements["color1"].selectedIndex;
        ....
        colorArray[0]=frm.elements["color1"].options[box1Index].value;

        ....[color=blue]
        > document.write( '<FONT COLOR="black">' );[/color]
        Don't do that.
        document.write can cause odd problems with an already open document.
        Instead, use a div and innerHTML or DOM methods createElement and
        appendChild.

        ....
        [color=blue]
        > <form name=form1 onsubmit="valid ateOnSubmit(thi s.form)">[/color]
        should be
        <form name="form1" onsubmit="retur n validateOnSubmi t(this.form)">
        The function validateOnSubmi t should return true to submit and false to
        cancel.

        Your object attributes should all be quoted. You have invalid HTML that IE
        will accept, but other browsers will puke on. Options must have end tags.
        <option>somethi ng</option>
        You have some that do and some that don't.
        Go validate your page with the W3C. This looks like something made in
        Frontpage. IOW, invalid HTML. ;)

        ....[color=blue]
        > onClick=display Colors(this.for m)[/color]
        should be
        onClick="displa yColors(this.fo rm)"

        As to the validation you asked about, all your selects have an option as the
        first one (selectedIndex= 0) that basically tells the user to select a color.
        Just have your validator check that for each select the selectedIndex is not
        0.

        <SELECT id="color2" style="WIDTH: 180px" name="color2">
        <OPTION value = "0"> Select a color </option>

        the function has something like

        function validateOnSubmi t(frm)
        {
        if (frm.elements["color2"].selectedIndex == 0)
        {
        alert("You must select an option for color2.");
        return false;
        }
        // put more checks here

        // get here, all passed
        return true;
        }


        HTH
        --
        --
        ~kaeli~
        Well, aren't we just a flipping ray of sunshine?



        Comment

        • Ann D.

          #5
          Re: *Please Help*

          Thanks Kaeli! Your input was very helpful :) The only thing that I'm
          not clear on is:

          ....[color=blue]
          > document.write( '<FONT COLOR="black">' );[/color]
          Don't do that.
          document.write can cause odd problems with an already open document.
          Instead, use a div and innerHTML or DOM methods createElement and
          appendChild.

          As mentioned, I'm new at this. If you can, I would love an example of
          this.
          Anyway, thanks again for your help. I'm learning lots.
          Ann
          Ottawa ON Canada


          kaeli <tiny_one@NOSPA M.comcast.net> wrote in message news:<MPG.1b83d 204dd783ca598a0 30@nntp.lucent. com>...[color=blue]
          > In article <__lSc.373267$r CA1.343910@news 01.bloor.is.net .cable.rogers.c om>,
          > annduguayNOSPAM @yahoo.ca enlightened us with...[color=green]
          > > Hi Robert!
          > > Nope.. not a school project :-) I'm getting a little too old for school.
          > > I'm trying to help out a friend that's doing something in HTML but doesn't
          > > have any javascript knowledge. After looking at some code, I thought it
          > > would be easy but I'm having a difficult time.
          > >
          > > Her project is a little more complex as it has 55 questions with select
          > > boxes for each and every questions needs to be answered. Upon completion, a
          > > score is calculated based on the answers the user gives and a color report
          > > is printed to the screen (color also is score dependent).
          > >
          > > Hence, my small piece of code. I figured I'd start small but I'm stuck with
          > > the validation. All I want to do is to alert the user when the user hasn't
          > > selected anything (or the value is 0).
          > >[/color]
          >
          > I'll give you some hints.
          >[color=green]
          > > box1Index=docum ent.form1.color 1.selectedIndex ;[/color]
          > That is not as cross-browser as
          > box1Index=docum ent.forms["form1"].elements["color1"].selectedIndex;
          >
          > Now, your function has the form passed in, so you can do
          > function displayColors(f rm)
          > Note that I changed "form" to "frm" because I have this thing about having a
          > variable with the same name as an already defined object ("form" is already
          > an object in JS).
          >
          > So, you passed in "frm", so
          > box1Index=frm.e lements["color1"].selectedIndex;
          > ...
          > colorArray[0]=frm.elements["color1"].options[box1Index].value;
          >
          > ...[color=green]
          > > document.write( '<FONT COLOR="black">' );[/color]
          > Don't do that.
          > document.write can cause odd problems with an already open document.
          > Instead, use a div and innerHTML or DOM methods createElement and
          > appendChild.
          >
          > ...
          >[color=green]
          > > <form name=form1 onsubmit="valid ateOnSubmit(thi s.form)">[/color]
          > should be
          > <form name="form1" onsubmit="retur n validateOnSubmi t(this.form)">
          > The function validateOnSubmi t should return true to submit and false to
          > cancel.
          >
          > Your object attributes should all be quoted. You have invalid HTML that IE
          > will accept, but other browsers will puke on. Options must have end tags.
          > <option>somethi ng</option>
          > You have some that do and some that don't.
          > Go validate your page with the W3C. This looks like something made in
          > Frontpage. IOW, invalid HTML. ;)
          >
          > ...[color=green]
          > > onClick=display Colors(this.for m)[/color]
          > should be
          > onClick="displa yColors(this.fo rm)"
          >
          > As to the validation you asked about, all your selects have an option as the
          > first one (selectedIndex= 0) that basically tells the user to select a color.
          > Just have your validator check that for each select the selectedIndex is not
          > 0.
          >
          > <SELECT id="color2" style="WIDTH: 180px" name="color2">
          > <OPTION value = "0"> Select a color </option>
          >
          > the function has something like
          >
          > function validateOnSubmi t(frm)
          > {
          > if (frm.elements["color2"].selectedIndex == 0)
          > {
          > alert("You must select an option for color2.");
          > return false;
          > }
          > // put more checks here
          >
          > // get here, all passed
          > return true;
          > }
          >
          >
          > HTH
          > --[/color]

          Comment

          • Robert

            #6
            Re: *Please Help*

            "Ann Duguay" <annduguayNOSPA M@yahoo.ca> wrote in message news:<__lSc.373 267$rCA1.343910 @news01.bloor.i s.net.cable.rog ers.com>...[color=blue]
            > Hi Robert!
            > Nope.. not a school project :-) I'm getting a little too old for school.
            > I'm trying to help out a friend that's doing something in HTML but doesn't
            > have any javascript knowledge. After looking at some code, I thought it
            > would be easy but I'm having a difficult time.[/color]

            Programming with Javascript in a web browser requires learning a lot.
            Just because Javascript doesn't have pointers doesn't make programming
            easy.

            I suggest programming in Netscape 7.1 or firefox. Once you have
            things working test in IE. The best book on javascript is
            "JavaScript : The definitive guide" by David Flanagan.

            Once you get tired of the alert boxes, see my post:


            It wrapped. See the second post on Re: Debug - Your own console.[color=blue]
            >
            > Her project is a little more complex as it has 55 questions with select
            > boxes for each and every questions needs to be answered. Upon completion, a
            > score is calculated based on the answers the user gives and a color report
            > is printed to the screen (color also is score dependent).
            >[/color]

            This may get you started. I tested this in Netscape 7.1 and IE 5.2 on
            MacOS 10.2.6. Be sure to have popup windows enabled!

            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
            <HTML>
            <HEAD>
            <Title>My color test</Title>
            <script type="text/javascript">

            function displayColors(f orm)
            {
            alert("in dislayColors. Display results in a popup window.");
            var aDate = new Date();
            var colorList = ["none","blue"," red","black"];
            var windowName = "myWindowColor" ;
            newWindow = window.open("", windowName,
            "scrollbars=yes ,resizable=yes, width=700,heigh t=500");

            newWindow.docum ent.writeln(
            '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
            Transitional//EN">');
            newWindow.docum ent.writeln(
            "<html>");
            newWindow.docum ent.writeln(
            "<header><title >Window " + windowName + "</title></header>");
            newWindow.docum ent.writeln(
            "<body><p>T he information that follows was generated on "
            + aDate + "</p>");
            for (var i=1; i<=3; i++)
            {
            var boxColor=form.e lements["color" + i].value;

            newWindow.docum ent.writeln(
            "<p><span style='color:" + colorList[boxColor] + "';>" +
            "You picked color "+ colorList[boxColor] +
            " for input number " + i + "." +
            "</span></p>");
            }

            return true;
            } //end function

            function processColors(f orm)
            {
            alert("in processColors." );
            // form contains a reference to the current form
            var boxColor;

            for (var i=1; i<=3; i++)
            {
            boxColor=form.e lements["color" + i].value;
            alert("boxColor = " + boxColor);
            if (boxColor == 0 )
            {
            alert("You need to pick a color for " + i + ".");
            return false;
            }
            }
            displayColors(f orm);
            return true;
            } //end function

            function validateOnSubmi t(form)
            {
            alert("in validateOnSubmi t.");
            }

            </script>
            </HEAD>
            <BODY>
            <!-- I believe that you need to have a HTML submit button
            for the form to be submitted and your onsubmit event
            handler to get control. -->
            <form name="form1"
            onsubmit="valid ateOnSubmit(thi s.form)">
            Pick color 1:
            <SELECT name="color1" name=color1 style="WIDTH: 180px">
            <OPTION value = "0">Select a color
            <OPTION value = "1" name="blue">Blu e
            <option value = "2" name="red">Red
            <option value = "3" name="black">Bl ack
            </OPTION></SELECT>
            <br><br>
            Pick color 2:
            <!-- I avoid having duplicate id & name names.
            There is a lot of confusion of when to use
            id and name. I am confused too. name seems
            to be the one used
            for forms. id seems to be the newer one.
            Don't know what happens when use both with the same
            name. -->
            <SELECT name="color2" style="WIDTH: 180px">
            <OPTION value = "0"> Select a color
            <OPTION value = "1" name="blue">Blu e
            <option value = "2" name="red">Red
            <option value = "3" name="black">Bl ack
            </OPTION></SELECT>
            <br><br>
            Pick color 3:
            <SELECT name="color3" style="width: 180px">
            <OPTION value = "0"> Select a color
            <OPTION value = "1" name="blue">Blu e
            <option value = "2" name="red" >Red
            <option value = "3" name="black">Bl ack
            </OPTION></SELECT>
            <br>
            <input TYPE=BUTTON NAME="cmdCalc" VALUE="GO"
            onClick="proces sColors(this.fo rm)">

            </BODY>
            </HTML>


            Robert

            Comment

            • kaeli

              #7
              Re: *Please Help*

              In article <aed83d34.04081 11043.68df6ac9@ posting.google. com>,
              annduguay@yahoo .ca enlightened us with...[color=blue]
              > Thanks Kaeli! Your input was very helpful :) The only thing that I'm
              > not clear on is:
              >
              > ...[color=green]
              > > document.write( '<FONT COLOR="black">' );[/color]
              > Don't do that.
              > document.write can cause odd problems with an already open document.
              > Instead, use a div and innerHTML or DOM methods createElement and
              > appendChild.
              >
              > As mentioned, I'm new at this. If you can, I would love an example of
              > this.
              > Anyway, thanks again for your help. I'm learning lots.[/color]

              Search the archives for people who did it and it caused their problem. *grin*

              Long link:

              &q=javascript+d ocument.write+w iped+out&btnG=S earch

              tiny:


              'document.write ' is a bad way to do pretty much anything. It can work in some
              situations (writing dynamic content to brand new windows, such as popups, it
              works just fine) and is the only way to get dynamic content in Netscape 4,
              but since it's dicey and NN4 is old, it's pretty much outlived its
              usefulness.
              The thing is, you can often use it to write to a layer without killing
              things, but a lot of times it ends up not doing what you wanted it to do.
              It's kind of like "eval" - most of the time it's used, it causes more
              problems than it solves.

              If you are at all familiar with C, it's kind of like using a pointer (char *)
              when a character array (char[50]) will do. Both can work, but one can really
              get you into trouble if you don't use it right. *g*

              HTH
              --
              --
              ~kaeli~
              Once you've seen one shopping center, you've seen a mall.



              Comment

              Working...