REQ: New Question Form ( new JavaScript Student )

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

    #1

    REQ: New Question Form ( new JavaScript Student )

    In this code why is it that when I press the SUBMIT button the focus
    only goes back to the Numeric field. What do I need to do to correct
    this problem?

    Sue




    <html>

    <head>
    <title>JavaScri pt Project</title>
    <SCRIPT LANGUAGE="JAVAS CRIPT">
    <!--Hide from old browsers
    function Validate() {
    var themessage = "You are required to complete the following fields:
    ";

    // validate the Firstname
    if (document.Regis ter.FirstName.v alue=="") {
    alert("Please enter your Firstname");
    document.Regist er.FirstName.va lue="";
    document.Regist er.FirstName.fo cus();
    }

    else {
    // validate the Lastname
    if (document.Regis ter.LastName.va lue=="") {
    alert("Please enter your Lastname");
    document.Regist er.LastName.val ue="";
    document.Regist er.LastName.foc us();
    }
    else {

    // validate Age as be numeric
    var YearsOld=docume nt.Register.Age .value;
    var YearsOld=parseI nt(YearsOld,10) ;
    if (isNaN(YearsOld )) {
    alert("Age is not numeric");
    document.Regist er.Age.value="" ;
    document.Regist er.Age.focus();
    }
    else {

    // validate the @ sign and the period as being the fourth
    from the last character in an e-mail address
    var RegeMail=docume nt.Register.ema il.value;
    var atSign = RegeMail.indexO f("@");

    var Period=document .Register.email .value;
    var PPeriod = Period.indexOf( '.');
    var LPeriod = Period.length - 4;

    if (RegeMail == "" || atSign == -1 || LPeriod !=
    PPeriod) {
    alert("Please enter a valid e-mail address");
    document.Regist er.email.value = "";
    document.Regist er.email.focus( );
    }
    else{

    // validate the Gender in a drop down menu
    var sex=document.fo rms[0].Gender.selecte dIndex;
    if (sex==0) {
    alert("You must select your GENDER from the drop-down
    Menu.");
    document.forms[0].Gender.focus() ;
    }
    else
    {

    //alert if fields are empty and cancel form submit
    if (themessage == "You are required to complete the following fields:
    ") {
    document.Regist er.submit();
    }
    else {
    alert(themessag e);
    return false;
    }
    }
    }
    }
    }
    }
    }

    //-->
    </script>
    </head>

    <body>

    <center>

    <form name=Register method="post" action="">
    <table border="0" width="90%">

    <!-- Begining of the first line of the form for entering data. -->
    <tr>
    <td>Enter Your Firstname :</td><td align="center">
    <Input Type="text" Name="FirstName " value=" "></td>
    <td>&nbspEnte r Your Age :</td> <td align="center">
    <Input Type="numeric" Name="Age" value=" "></td>
    <td align="center"> Select your : <SELECT NAME="Gender" SIZE=1 >
    <OPTION SELECTED VALUE=""> --- Select Gender ---
    <OPTION VALUE="Male">Ma le
    <OPTION VALUE="Female"> Female
    </SELECT>
    </td>
    </tr>
    <!-- ending of the first line of the form for entering data. -->
    <tr>
    <td></td>
    </tr>

    <!-- Begining of the second line of the form for entering data. -->
    <tr>
    <td align="center"> Enter Your Lastname :</td><td align="center">
    <Input Type="text" Name="LastName" value=" "></td>
    <td align="center"> Enter Your Email Address :</td> <td
    align="center">
    <Input Type="text" Name="email" value=" "></td>
    <td align="right">< input type=button value="Submit Request"
    onclick="Valida te();">
    <Input Type="Reset"></td>
    </tr>
    <!-- ending of the second line of the form for entering data. -->

    </body>
    </html>
  • Lee

    #2
    Re: REQ: New Question Form ( new JavaScript Student )

    Sue said:[color=blue]
    >
    >In this code why is it that when I press the SUBMIT button the focus
    >only goes back to the Numeric field. What do I need to do to correct
    >this problem?[/color]

    In the HTML, you set the initial value of the fields to a single space
    character:
    [color=blue]
    > <Input Type="text" Name="LastName" value=" "></td>[/color]

    Your Validate() code checks to see if the fields are empty.
    A single space character is not the same as an empty field.
    Change your HTML value attributes to "".

    Comment

    • Sue

      #3
      Re: REQ: New Question From ( new JavaScript Student )

      On 10 Dec 2003 08:16:18 -0800, Lee <REM0VElbspamtr ap@cox.net> wrote:
      [color=blue]
      >Sue said:[color=green]
      >>
      >>In this code why is it that when I press the SUBMIT button the focus
      >>only goes back to the Numeric field. What do I need to do to correct
      >>this problem?[/color]
      >
      >In the HTML, you set the initial value of the fields to a single space
      >character:
      >[color=green]
      >> <Input Type="text" Name="LastName" value=" "></td>[/color]
      >
      >Your Validate() code checks to see if the fields are empty.
      >A single space character is not the same as an empty field.
      >Change your HTML value attributes to "".[/color]

      Hey Lee,

      Thanks for explaining that to me. Now one last question before I have
      to turn this project in on Friday.

      This is not necessary for my project but I have created a function to
      do more extensive checking for numbers in the Age field. However, I am
      unable to insert this function into the code without it causing
      problems. Is there an easy fool proof way to call this function
      without a lot of difficulty. Thanks

      Sue


      function CheckAge(Age) {
      var valid = "0123456789 "
      var Good = "yes";
      var Check;
      for (var i=0; i<Age.value.len gth; i++) {
      Check = "" + Age.value.subst ring(i, i+1);
      if (valid.indexOf( Check) == "-1") Good = "no";
      }
      if (Good == "no") {
      alert("An INVALID Character has been entered for your age !");
      document.Regist er.Age.value="" ;
      document.Regist er.Age.focus();
      }
      }

      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: REQ: New Question From ( new JavaScript Student )

        Sue wrote:
        [color=blue]
        > This is not necessary for my project but I have created a function to
        > do more extensive checking for numbers in the Age field. However, I am
        > unable to insert this function into the code without it causing
        > problems. Is there an easy fool proof way to call this function
        > without a lot of difficulty. Thanks
        > [...]
        > function CheckAge(Age) {[/color]

        function checkAge(age)
        {

        (Note that the case changed for good style.)
        [color=blue]
        > var valid = "0123456789 "
        > var Good = "yes";[/color]

        Do not use strings for booleans, use booleans.

        var good = true;

        But you will not need that initialization and the `valid'
        variable if you use Regular Expressions and write
        [color=blue]
        > var Check;[/color]

        var check;
        [color=blue]
        > for (var i=0; i<Age.value.len gth; i++) {
        > Check = "" + Age.value.subst ring(i, i+1);[/color]

        check = age.charAt(i);

        is, although not required for the RegExp solution, far more simple.
        Even with String.substrin g(...) the concatenation with "" is not
        required since String.substrin g(...) already returns a `string' value.
        [color=blue]
        > if (valid.indexOf( Check) == "-1") Good = "no";
        > }[/color]

        var good = /^\d+$/.test(age); // Only one or more decimal digit?
        [color=blue]
        > if (Good == "no") {[/color]

        if (! good)
        {
        [color=blue]
        > alert("An INVALID Character has been entered for your age !");
        > document.Regist er.Age.value="" ;
        > document.Regist er.Age.focus();[/color]

        var oAge = document.forms["Register"].elements["Age"];
        oAge.value = "";
        oAge.focus();
        }
        [color=blue]
        > }
        > }[/color]


        HTH

        PointedEars

        Comment

        • Sue

          #5
          Re: REQ: New Question From ( new JavaScript Student )

          On Thu, 11 Dec 2003 22:30:03 +0100, Thomas 'PointedEars' Lahn
          <PointedEars@we b.de> wrote:
          [color=blue]
          >Sue wrote:
          >[color=green]
          >> This is not necessary for my project but I have created a function to
          >> do more extensive checking for numbers in the Age field. However, I am
          >> unable to insert this function into the code without it causing
          >> problems. Is there an easy fool proof way to call this function
          >> without a lot of difficulty. Thanks
          >> [...]
          >> function CheckAge(Age) {[/color]
          >[/color]


          <snip>

          Thomas,

          I tried to make the changes you suggested but some of this is over my
          head. The charAt(i) has not been in any of the chapters that I have
          had to do and I have done every chapter in the book this quarter. For
          self gratification I would like to be able to plug this code into my
          project but I have been unable to plug it in with out it causing
          problems and my time is up tomorrow (Friday).



          function checkAge(age) {
          var valid = "0123456789 "
          var good = true;
          var check;
          for (var i=0; i<Age.value.len gth; i++) {
          check = age.charAt(i);
          var good = /^\d+$/.test(age); // Only one or more decimal digit?
          }
          if (! good) {
          alert("An INVALID Character has been entered for your age !");
          var oAge = document.forms["Register"].elements["Age"];
          oAge.value = "";
          oAge.focus();
          }
          }

          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: REQ: New Question From ( new JavaScript Student )

            [posted & mailed (the latter accidentally, though)]

            Sue wrote:
            [color=blue]
            > I tried to make the changes you suggested but some of this is over my
            > head. The charAt(i) has not been in any of the chapters that I have
            > had to do and I have done every chapter in the book this quarter.[/color]

            It looks like you have one of the plenty of badly written JavaScript
            books out there (see also the FAQ, there is only *one* which can be
            recommended): charAt(...) is a *basic* method of the String prototype,
            returning the character of the respective String (object) at the index
            `i' (where the index is zero-based). Throw the book away and read the
            specs instead (as you see, you can download the entire Guide/Reference
            to your local hard disk; I have reconfigured my local Web server for
            quick access which I can recommend you, too):


            [color=blue]
            > For self gratification I would like to be able to plug this code into
            > my project but I have been unable to plug it in with out it causing
            > problems and my time is up tomorrow (Friday).[/color]

            Search the index of the JavaScript 1.x Core Guide/Reference, it is
            the fastest way to find what you are looking for. Here's the working
            function (only snipped the parts no longer necessary):

            function checkAge(age) {
            var good = /^\d+$/.test(age); // Only one or more decimal digit?
            if (! good)
            {
            alert("An INVALID Character has been entered for your age!");
            var oAge = document.forms["Register"].elements["Age"];
            oAge.value = "";
            oAge.focus();
            }
            }

            A little bit more optimization is possible, removing another unnecessary
            variable:

            function checkAge(age) {
            // Does it contain a character other than a decimal digit?
            if (! /^\d+$/.test(age))
            {
            alert("An INVALID Character has been entered for your age!");
            var oAge = document.forms["Register"].elements["Age"];
            oAge.value = "";
            oAge.focus();
            }
            }

            I guess you need that for form validation, so you would instead write:

            <script type="text/javascript">
            <!--
            function checkAge(oAge) { // pass a reference to the obj. 2b checked
            // Does it contain a character other than a decimal digit?
            if (! /^\d+$/.test(oAge.valu e))
            {
            alert("An INVALID Character has been entered for your age!");
            oAge.value = "";

            if (typeof oAge.focus == "function"
            || typeof oAge.focus == "object")
            {
            oAge.focus();
            }

            return false;
            }

            return true;
            }
            //-->
            </script>

            <form action="..." onsubmit="retur n checkAge(this.e lements['Age']);">
            ...
            <input name="Age">
            ...
            </form>

            You should always pass references to a form-processing function instead
            of primitive values as they are not that error-catching and you can use
            them also within the function, without retrieving them (via collections)
            again.


            HTH

            PointedEars
            --
            Dr. Weaver: What's with the cat?
            Cop in Morgue: Well, there's a problem with the cat. Sign here.
            Dr. Weaver: [signing] What's the problem with the cat?
            Cop in Morgue: It's your problem.

            Comment

            • Sue

              #7
              Re: REQ: New Question From ( new JavaScript Student )

              On Fri, 12 Dec 2003 05:35:49 +0100, Thomas 'PointedEars' Lahn
              <PointedEars@we b.de> wrote:
              [color=blue]
              >[posted & mailed (the latter accidentally, though)]
              >
              >Sue wrote:
              >[color=green]
              >> I tried to make the changes you suggested but some of this is over my
              >> head. The charAt(i) has not been in any of the chapters that I have
              >> had to do and I have done every chapter in the book this quarter.[/color]
              >
              >It looks like you have one of the plenty of badly written JavaScript
              >books out there (see also the FAQ, there is only *one* which can be
              >recommended) : charAt(...) is a *basic* method of the String prototype,
              >returning the character of the respective String (object) at the index
              >`i' (where the index is zero-based). Throw the book away and read the
              >specs instead (as you see, you can download the entire Guide/Reference
              >to your local hard disk; I have reconfigured my local Web server for
              >quick access which I can recommend you, too):
              >
              >http://devedge.netscape.com/library/manuals/
              >[color=green]
              >> For self gratification I would like to be able to plug this code into
              >> my project but I have been unable to plug it in with out it causing
              >> problems and my time is up tomorrow (Friday).[/color]
              >
              >Search the index of the JavaScript 1.x Core Guide/Reference, it is
              >the fastest way to find what you are looking for. Here's the working
              >function (only snipped the parts no longer necessary):
              >
              > function checkAge(age) {
              > var good = /^\d+$/.test(age); // Only one or more decimal digit?
              > if (! good)
              > {
              > alert("An INVALID Character has been entered for your age!");
              > var oAge = document.forms["Register"].elements["Age"];
              > oAge.value = "";
              > oAge.focus();
              > }
              > }
              >
              >A little bit more optimization is possible, removing another unnecessary
              >variable:
              >
              > function checkAge(age) {
              > // Does it contain a character other than a decimal digit?
              > if (! /^\d+$/.test(age))
              > {
              > alert("An INVALID Character has been entered for your age!");
              > var oAge = document.forms["Register"].elements["Age"];
              > oAge.value = "";
              > oAge.focus();
              > }
              > }
              >
              >I guess you need that for form validation, so you would instead write:
              >
              > <script type="text/javascript">
              > <!--
              > function checkAge(oAge) { // pass a reference to the obj. 2b checked
              > // Does it contain a character other than a decimal digit?
              > if (! /^\d+$/.test(oAge.valu e))
              > {
              > alert("An INVALID Character has been entered for your age!");
              > oAge.value = "";
              >
              > if (typeof oAge.focus == "function"
              > || typeof oAge.focus == "object")
              > {
              > oAge.focus();
              > }
              >
              > return false;
              > }
              >
              > return true;
              > }
              > //-->
              > </script>
              >
              > <form action="..." onsubmit="retur n checkAge(this.e lements['Age']);">
              > ...
              > <input name="Age">
              > ...
              > </form>
              >
              >You should always pass references to a form-processing function instead
              >of primitive values as they are not that error-catching and you can use
              >them also within the function, without retrieving them (via collections)
              >again.
              >
              >
              >HTH
              >
              >PointedEars[/color]


              Thanks Thomas,

              I check out your url and recommend to the instructor that he list is
              as an aid in future class.

              Sue

              Comment

              • Lasse Reichstein Nielsen

                #8
                Re: REQ: New Question From ( new JavaScript Student )

                Sue <SueWo@comcast. net > writes:
                [color=blue]
                > Thanks Thomas,[/color]

                Please reduce your quotes. There is no need to include 96 lines of quotes
                to add four lines of answer.
                [color=blue]
                > I check out your url and recommend to the instructor that he list is
                > as an aid in future class.[/color]

                One or two lines of quotes would be sufficient to give the context needed
                to understand your answer.

                /L
                --
                Lasse Reichstein Nielsen - lrn@hotpop.com
                DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                'Faith without judgement merely degrades the spirit divine.'

                Comment

                • Thomas 'PointedEars' Lahn

                  #9
                  Re: REQ: New Question From ( new JavaScript Student )

                  Sue wrote:
                  [color=blue]
                  > [...] Thomas 'PointedEars' Lahn [...] wrote:[color=green]
                  >> [...]
                  >> Sue wrote:[color=darkred]
                  >>> I tried to make the changes you suggested but some of this is over my
                  >>> head. The charAt(i) has not been in any of the chapters that I have
                  >>> had to do and I have done every chapter in the book this quarter.[/color]
                  >>
                  >> It looks like you have one of the plenty of badly written JavaScript
                  >> books out there (see also the FAQ, there is only *one* which can be
                  >> recommended): charAt(...) is a *basic* method of the String prototype,
                  >> returning the character of the respective String (object) at the index
                  >> `i' (where the index is zero-based). Throw the book away and read the
                  >> specs instead (as you see, you can download the entire Guide/Reference
                  >> to your local hard disk; I have reconfigured my local Web server for
                  >> quick access which I can recommend you, too):
                  >>
                  >> http://devedge.netscape.com/library/manuals/
                  >> [...][/color]
                  >
                  > Thanks Thomas,[/color]

                  You are welcome, but please trim your quotes to the part(s) you are
                  actually referring to the next time. Time, bandwidth and disk space
                  are precious resources.
                  [color=blue]
                  > I check out your url and recommend to the instructor that he list is
                  > as an aid in future class.[/color]

                  Good idea.


                  PointedEars

                  Comment

                  Working...