REQ: Could Someone assist in correcting this code ::::::::>>>(New JavaScript Student)

Discussion in 'Javascript' started by Sue, Dec 9, 2003.

  1. Sue

    Sue Guest

    Hello! I am back with another question.

    Remember I am a new JavaScript student and I am aware that this code
    does not check for all the possibilities and that as a "NEW"
    JavaScript student I am not expected to check for everything.

    At any rate, the problem I am having with the following code is that
    it does not clear the fields once I press the SEND button. So can
    anyone here enlighten me as to what is causing the problem.

    **************************************************************************



    <html>

    <head>
    <title>JavaScript Project</title>
    <SCRIPT LANGUAGE="JAVASCRIPT">
    <!--Hide from old browsers
    function Validate(DataEntry) {
    // validate the Firstname
    var FstName=document.Register.FirstName.value
    if (FstName == " ") {
    alert("Please enter your Firstname")
    document.Register.FirstName.value=" "
    document.Register.FirstName.focus()
    }
    else {

    // validate the Lastname
    var LstName=document.Register.LastName.value
    if (LstName == " ") {
    alert("Please enter your Lastname")
    document.Register.LastName.value=" "
    document.Register.LastName.focus()
    }
    else {

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

    // validate the @ sign and the period as being the fourth
    from the last character in an e-mail address
    var RegeMail=document.Register.eMail.value
    var atSign = RegeMail.indexOf("@")

    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.Register.eMail.value = " "
    document.Register.eMail.focus()
    }
    else {

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

    Gender_selection=document.forms[0].Gender.options[sex].value;
    return true;
    }
    }
    }
    }
    }
    }


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

    <body>
    <FORM Name="Register">
    <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>&nbspEnter 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">Male
    <OPTION VALUE="Female">Female
    </SELECT>
    </td>
    </tr>
    <!-- ending of the first line of the form for entering data. -->

    <!-- 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="Send"
    onClick="Validate(Register)">
    <Input
    Type="Reset">&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</td>
    </tr>
    <!-- ending of the second line of the form for entering data. -->

    </table>
    </form>
    </body>
    </html>
     
    Sue, Dec 9, 2003
    #1
    1. Advertising

  2. Sue

    Lee Guest

    Sue said:
    >
    >
    >Hello! I am back with another question.
    >
    >Remember I am a new JavaScript student and I am aware that this code
    >does not check for all the possibilities and that as a "NEW"
    >JavaScript student I am not expected to check for everything.
    >
    >At any rate, the problem I am having with the following code is that
    >it does not clear the fields once I press the SEND button. So can
    >anyone here enlighten me as to what is causing the problem.


    > <Input Type="button" Value="Send" onClick="Validate(Register)">


    It's not clear why you expect the fields to be cleared.
    Your "Send" button doesn't actually send any data.
    It just executes your Validate() function.

    If you want it to send the data, you should use an input of
    type "submit", instead of "button", and instead of using the
    onclick event handler of the button, you should execute your
    Validate() function within the onsubmit event handler of the
    form.
     
    Lee, Dec 9, 2003
    #2
    1. Advertising

  3. Lee wrote on 09 Dec 2003 at Tue, 09 Dec 2003 03:26:39 GMT:

    > Sue said:


    >> <Input Type="button" Value="Send"
    >> onClick="Validate(Register)">

    >
    > If you want it to send the data, you should use an input of
    > type "submit", instead of "button", and instead of using the
    > onclick event handler of the button, you should execute your
    > Validate() function within the onsubmit event handler of the
    > form.


    You'll also want to make sure that you use the return keyword so that
    the result of the validation allows or cancels the submission.

    Mike

    --
    Michael Winter
    d (replace ".invalid" with ".uk")
     
    Michael Winter, Dec 9, 2003
    #3
  4. Sue

    Sue Guest

    On 8 Dec 2003 19:26:39 -0800, Lee <> wrote:

    >Sue said:
    >>
    >>
    >>Hello! I am back with another question.
    >>
    >>Remember I am a new JavaScript student and I am aware that this code
    >>does not check for all the possibilities and that as a "NEW"
    >>JavaScript student I am not expected to check for everything.
    >>
    >>At any rate, the problem I am having with the following code is that
    >>it does not clear the fields once I press the SEND button. So can
    >>anyone here enlighten me as to what is causing the problem.

    >
    >> <Input Type="button" Value="Send" onClick="Validate(Register)">

    >
    >It's not clear why you expect the fields to be cleared.
    >Your "Send" button doesn't actually send any data.
    >It just executes your Validate() function.
    >
    >If you want it to send the data, you should use an input of
    >type "submit", instead of "button", and instead of using the
    >onclick event handler of the button, you should execute your
    >Validate() function within the onsubmit event handler of the
    >form.



    Lee,

    I am having to take this JavaScript class over the Internet and other
    than the textbox, I have no help. Therefore, I am not sure about what
    you are talking about but I tried SUBMIT and it does clear the fields.
    However, if invalid data is entered and I press Send, all fields are
    cleared and the info has to be reentered.

    If you could make the changes to the code that you mentioned and
    post it I would greatly appreciate it.

    Thanks.

    Sue
     
    Sue, Dec 9, 2003
    #4
  5. Sue

    Brian Guest

    "Sue" < > wrote in message
    news:...
    > On 8 Dec 2003 19:26:39 -0800, Lee <> wrote:
    >
    > >Sue said:
    > >>
    > >>
    > >>Hello! I am back with another question.
    > >>
    > >>Remember I am a new JavaScript student and I am aware that this code
    > >>does not check for all the possibilities and that as a "NEW"
    > >>JavaScript student I am not expected to check for everything.
    > >>
    > >>At any rate, the problem I am having with the following code is that
    > >>it does not clear the fields once I press the SEND button. So can
    > >>anyone here enlighten me as to what is causing the problem.

    > >
    > >> <Input Type="button" Value="Send" onClick="Validate(Register)">

    > >
    > >It's not clear why you expect the fields to be cleared.
    > >Your "Send" button doesn't actually send any data.
    > >It just executes your Validate() function.
    > >
    > >If you want it to send the data, you should use an input of
    > >type "submit", instead of "button", and instead of using the
    > >onclick event handler of the button, you should execute your
    > >Validate() function within the onsubmit event handler of the
    > >form.

    >
    >
    > Lee,
    >
    > I am having to take this JavaScript class over the Internet and other
    > than the textbox, I have no help. Therefore, I am not sure about what
    > you are talking about but I tried SUBMIT and it does clear the fields.
    > However, if invalid data is entered and I press Send, all fields are
    > cleared and the info has to be reentered.
    >
    > If you could make the changes to the code that you mentioned and
    > post it I would greatly appreciate it.
    >
    > Thanks.
    >
    > Sue
    >


    Hmmmm... Asking a newsgroup to fix your homework? Is that ethical? Part of
    the learning process is using resources to find solutions by your self....

    B
     
    Brian, Dec 9, 2003
    #5
  6. Sue

    Lee Guest

    Brian said:
    >
    >
    >"Sue" < > wrote:


    >> If you could make the changes to the code that you mentioned and
    >> post it I would greatly appreciate it.


    >Hmmmm... Asking a newsgroup to fix your homework? Is that ethical? Part of
    >the learning process is using resources to find solutions by your self....


    It's certainly more ethical than the students who try to pretend
    that it isn't homework: "My client would like a form that allows
    the visitor to type his name and have the message 'Hello, <name>!'
    appear in a popup alert box."

    I don't mind answering questions or giving tips, but I won't go
    so far as to complete the assignment.

    Here's a simple example of form validation. In production,
    I would use a regular expression to test to see if the field
    contains valid data, but that's beyond the scope of this
    assignment.

    <html>
    <head>
    <script type="text/javascript">
    function Validate(f){
    // f is a reference to the form to be validated.
    // This function returns true if all fields are valid
    // or false if any are invalid.

    if(f.FirstName.value==""){ // Note: no space between quotes
    alert("Please enter a value for First Name");
    f.FirstName.focus();
    return false;
    }
    if(f.LastName.value==""){
    alert("Please enter a value for Last Name");
    f.LastName.focus();
    return false;
    }

    return true; // All fields are valid.

    }
    </script>
    </head>
    <body>
    <form onsubmit="return Validate(this)">
    First Name: <input name="FirstName"><br>
    Last Name: <input name="LastName"><br>
    <input type="Submit" value="Send">
    </form>
    </body>
     
    Lee, Dec 9, 2003
    #6
  7. Sue

    Sue Guest

    On 9 Dec 2003 09:05:31 -0800, Lee <> wrote:

    >Brian said:
    >>
    >>
    >>"Sue" < > wrote:

    >
    >>> If you could make the changes to the code that you mentioned and
    >>> post it I would greatly appreciate it.

    >
    >>Hmmmm... Asking a newsgroup to fix your homework? Is that ethical? Part of
    >>the learning process is using resources to find solutions by your self....

    >
    >It's certainly more ethical than the students who try to pretend
    >that it isn't homework: "My client would like a form that allows
    >the visitor to type his name and have the message 'Hello, <name>!'
    >appear in a popup alert box."
    >
    >I don't mind answering questions or giving tips, but I won't go
    >so far as to complete the assignment.
    >
    >Here's a simple example of form validation. In production,
    >I would use a regular expression to test to see if the field
    >contains valid data, but that's beyond the scope of this
    >assignment.
    >




    Look guys I am not wanting to get into a ethical debate, but in most
    all of the computer related classes I have taken "in the classroom"
    the instructor has said, "I do not have time to provide all the help
    everyone needs so if you can help your neighbor please do so."

    Since I am not in class with classmates and the instructor stays
    confused as to who sent what email I thought I would ask for help
    here. I feel that if I were asking someone to do the complete
    assignment that would be unethical. As you can see I am not asking
    that. I am only asking for help on a small part and the code that I
    posted is probably only one-fourth of my overall project.

    Thanks for you help Lee. I will see if I can fix my problem.

    Sue
     
    Sue, Dec 10, 2003
    #7
  8. "Sue" < > wrote in message
    news:...
    <snip>
    >... and the instructor stays
    >confused as to who sent what email ...


    Given everything you have previously mentioned (such as the requirement
    to write e-mail address validators that have zero relation to how the
    task must be approached in the real world) it might be time to name the
    outfit providing this online "training". So they can be associated with
    the consequences of the quality of service they provide (particularly in
    google searches).

    Richard.
     
    Richard Cornford, Dec 10, 2003
    #8
    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. Joshua
    Replies:
    8
    Views:
    6,119
    Joshua
    Sep 29, 2004
  2. Sue
    Replies:
    8
    Views:
    149
    Thomas 'PointedEars' Lahn
    Dec 13, 2003
  3. Sue
    Replies:
    5
    Views:
    176
  4. O_TEXT
    Replies:
    1
    Views:
    121
    O_TEXT
    Jul 29, 2009
  5. Replies:
    3
    Views:
    78
    Kimmo Laine
    Aug 1, 2005
Loading...

Share This Page