New user- function doesn't work

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

    New user- function doesn't work

    Hello,
    I would appreciate any help I can get from anyone who has experience
    using javascript. I have an html form containing a number of functions
    controlling the submission of data to our database. I now want to
    require users to fill in three specific fields before they can submit
    the form. I found a function and modified it with my form name
    (mainform), and field names(sub 1,2 and 3)
    In the head of the document, I added this function to the existing ones
    that work successfully:

    function required()
    {
    if (document.mainf orm.sub1.value= =" ")
    {
    alert("Please Enter Name!");
    document.mainfo rm.sub1.focus()
    return false;}
    return true;

    if (document.mainf orm.sub2.value= =" ")
    {
    alert("Please Enter Position");
    document.mainfo rm.sub2.focus()
    return false;}
    return true;

    if (document.mainf orm.sub3.value= =" ")
    {
    alert("Please Enter Email Address");
    document.mainfo rm.sub3.focus()
    return false;}
    return true;
    }

    In the body I have

    form name ="=mainform"

    The other functions that work prevent submission under certain
    conditions, but my new function is just ignored. Does anyone have any
    suggestions?
    Thanks, kdc

  • Randy Webb

    #2
    Re: New user- function doesn't work

    kdc wrote:[color=blue]
    > Hello,
    > I would appreciate any help I can get from anyone who has experience
    > using javascript. I have an html form containing a number of functions
    > controlling the submission of data to our database. I now want to
    > require users to fill in three specific fields before they can submit
    > the form. I found a function and modified it with my form name
    > (mainform), and field names(sub 1,2 and 3)
    > In the head of the document, I added this function to the existing ones
    > that work successfully:
    >
    > function required()
    > {
    > if (document.mainf orm.sub1.value= =" ")[/color]

    That is checking to see if sub1's value is a space. Not what I think you
    want.

    if (document.mainf orm.sub1.value == '')
    [color=blue]
    > {
    > alert("Please Enter Name!");
    > document.mainfo rm.sub1.focus()
    > return false;}
    > return true;[/color]

    Right here, the function is either going to return true or false, any
    following code will never be executed.
    [color=blue]
    > if (document.mainf orm.sub2.value= =" ")
    > {
    > alert("Please Enter Position");
    > document.mainfo rm.sub2.focus()
    > return false;}
    > return true;
    >
    > if (document.mainf orm.sub3.value= =" ")
    > {
    > alert("Please Enter Email Address");
    > document.mainfo rm.sub3.focus()
    > return false;}
    > return true;
    > }[/color]

    function required(formRe f){
    var alertMessage = '';
    if (formRef.sub1.v alue = ''){alertMessag e += "Name Required\n";}
    if (formRef.sub2.v alue = ''){alertMessag e += "Position Required\n";}
    if (formRef.sub3.v alue = ''){alertMessag e += "Email Required\n";}
    if (alertMessage == ''){return true;}
    else{alert(aler tMessage);retur n false;}
    }

    [color=blue]
    > In the body I have
    >
    > form name ="=mainform"[/color]

    <form name="mainform" onsubmit="retur n required(this)" >

    --
    Randy
    comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly

    Comment

    • RobG

      #3
      Re: New user- function doesn't work

      Randy Webb wrote:[color=blue]
      > kdc wrote:
      >[color=green]
      >> Hello,
      >> I would appreciate any help I can get from anyone who has experience
      >> using javascript. I have an html form containing a number of functions
      >> controlling the submission of data to our database. I now want to
      >> require users to fill in three specific fields before they can submit
      >> the form. I found a function and modified it with my form name
      >> (mainform), and field names(sub 1,2 and 3)
      >> In the head of the document, I added this function to the existing ones
      >> that work successfully:
      >>
      >> function required()
      >> {
      >> if (document.mainf orm.sub1.value= =" ")[/color]
      >
      >
      > That is checking to see if sub1's value is a space. Not what I think you
      > want.
      >
      > if (document.mainf orm.sub1.value == '')
      >[color=green]
      >> {
      >> alert("Please Enter Name!");
      >> document.mainfo rm.sub1.focus()
      >> return false;}
      >> return true;[/color]
      >
      >
      > Right here, the function is either going to return true or false, any
      > following code will never be executed.
      >[color=green]
      >> if (document.mainf orm.sub2.value= =" ")
      >> {
      >> alert("Please Enter Position");
      >> document.mainfo rm.sub2.focus()
      >> return false;}
      >> return true;
      >>
      >> if (document.mainf orm.sub3.value= =" ")
      >> {
      >> alert("Please Enter Email Address");
      >> document.mainfo rm.sub3.focus()
      >> return false;}
      >> return true;
      >> }[/color]
      >
      >
      > function required(formRe f){
      > var alertMessage = '';
      > if (formRef.sub1.v alue = ''){alertMessag e += "Name Required\n";}[/color]

      A good argument for always putting literals on the left when doing
      evaluations (unless assignment is actually intended). In this
      case, the value of formRef.sub1.va lue is set to '' and the test is
      always true (unless the assignment is 'fixed' by older browsers) and
      the content of the form element is effectively deleted. Try:

      if ( '' = formRef.sub1.va lue ){alertMessage += "Name Required\n";}

      will return an error, prompt fixing:

      if ( '' == formRef.sub1.va lue ){alertMessage += "Name Required\n";}
      [color=blue]
      > if (formRef.sub2.v alue = ''){alertMessag e += "Position Required\n";}
      > if (formRef.sub3.v alue = ''){alertMessag e += "Email Required\n";}[/color]

      ditto...

      --
      Rob

      Comment

      • kdc

        #4
        Re: New user- function doesn't work



        RobG wrote:[color=blue]
        > Randy Webb wrote:[color=green]
        > > kdc wrote:
        > >[color=darkred]
        > >> Hello,
        > >> I would appreciate any help I can get from anyone who has experience
        > >> using javascript. I have an html form containing a number of functions
        > >> controlling the submission of data to our database. I now want to
        > >> require users to fill in three specific fields before they can submit
        > >> the form. I found a function and modified it with my form name
        > >> (mainform), and field names(sub 1,2 and 3)
        > >> In the head of the document, I added this function to the existing ones
        > >> that work successfully:
        > >>
        > >> function required()
        > >> {
        > >> if (document.mainf orm.sub1.value= =" ")[/color]
        > >
        > >
        > > That is checking to see if sub1's value is a space. Not what I think you
        > > want.
        > >
        > > if (document.mainf orm.sub1.value == '')
        > >[color=darkred]
        > >> {
        > >> alert("Please Enter Name!");
        > >> document.mainfo rm.sub1.focus()
        > >> return false;}
        > >> return true;[/color]
        > >
        > >
        > > Right here, the function is either going to return true or false, any
        > > following code will never be executed.
        > >[color=darkred]
        > >> if (document.mainf orm.sub2.value= =" ")
        > >> {
        > >> alert("Please Enter Position");
        > >> document.mainfo rm.sub2.focus()
        > >> return false;}
        > >> return true;
        > >>
        > >> if (document.mainf orm.sub3.value= =" ")
        > >> {
        > >> alert("Please Enter Email Address");
        > >> document.mainfo rm.sub3.focus()
        > >> return false;}
        > >> return true;
        > >> }[/color]
        > >
        > >
        > > function required(formRe f){
        > > var alertMessage = '';
        > > if (formRef.sub1.v alue = ''){alertMessag e += "Name Required\n";}[/color]
        >
        > A good argument for always putting literals on the left when doing
        > evaluations (unless assignment is actually intended). In this
        > case, the value of formRef.sub1.va lue is set to '' and the test is
        > always true (unless the assignment is 'fixed' by older browsers) and
        > the content of the form element is effectively deleted. Try:
        >
        > if ( '' = formRef.sub1.va lue ){alertMessage += "Name Required\n";}
        >
        > will return an error, prompt fixing:
        >
        > if ( '' == formRef.sub1.va lue ){alertMessage += "Name Required\n";}
        >[color=green]
        > > if (formRef.sub2.v alue = ''){alertMessag e += "Position Required\n";}
        > > if (formRef.sub3.v alue = ''){alertMessag e += "Email Required\n";}[/color]
        >
        > ditto...
        >
        > --
        > Rob[/color]

        Comment

        • kdc

          #5
          Re: New user- function doesn't work

          Thanks to both of you for your quick feedback.
          This is what I added to my program:

          function required(mainfo rm){
          var alertMessage = '';
          if (' '==mainform.sub 1.value ){alertMessage += "Name Required\n";}
          if (' '==mainform.sub 2.value){alertM essage += " Position Required\n";}
          if (''==mainform.s ub3.value){aler tMessage += "Email Required\n";}
          if (alertMessage == ''){return true;}
          else{alert(aler tMessage);retur n false;}

          }


          I wasn't sure whether to use =mainform or ==mainform so I also tried it
          with a single =. It continues to ignore it.

          I don't want to waste your time so let me ask if the fact that I call
          the function by
          adding it to another function (checkValues) that is then called in upon
          submission matters.

          I added required(this) at the end of the list as below:
          function checkValues() {
          if (! ( checkMonths() && checkTimes() &&
          checkRoutes1() &&
          checkAges() && checkGenders() &&
          checkVars() && checkCauses() && checkPhases() &&
          checkPersons() &&
          checkFactors() && checkTypes() && checkErrors() &&
          required(this) ) ) {
          return false;
          }
          }

          I also tried it without the 'this' just in case.

          In the body:

          <FORM NAME="mainForm" onsubmit="retur n checkValues();"
          ACTION="readfil e.php" method="POST">

          Thanks again for your help!

          Comment

          • Randy Webb

            #6
            Re: New user- function doesn't work

            kdc wrote:[color=blue]
            > Thanks to both of you for your quick feedback.
            > This is what I added to my program:
            >
            > function required(mainfo rm){
            > var alertMessage = '';
            > if (' '==mainform.sub 1.value ){alertMessage += "Name Required\n";}[/color]

            You are checking to see if sub1 is a space, not what you wanted.
            [color=blue]
            > if (' '==mainform.sub 2.value){alertM essage += " Position Required\n";}[/color]

            Same there.
            [color=blue]
            > if (''==mainform.s ub3.value){aler tMessage += "Email Required\n";}
            > if (alertMessage == ''){return true;}
            > else{alert(aler tMessage);retur n false;}
            >
            > }
            >
            >
            > I wasn't sure whether to use =mainform or ==mainform so I also tried it
            > with a single =. It continues to ignore it.[/color]

            It should be a double ==. My original had the typo in it and got
            propogated by me copy/pasting it.
            [color=blue]
            > I don't want to waste your time so let me ask if the fact that I call
            > the function by
            > adding it to another function (checkValues) that is then called in upon
            > submission matters.[/color]

            Yes, it makes a huge difference.
            [color=blue]
            > I added required(this) at the end of the list as below:[/color]

            "this", in that context, refers to the function, not the form. But since
            you don't use the parameter, theres no point in adding it.
            [color=blue]
            > function checkValues() {
            > if (! ( checkMonths() && checkTimes() &&
            > checkRoutes1() &&
            > checkAges() && checkGenders() &&
            > checkVars() && checkCauses() && checkPhases() &&
            > checkPersons() &&
            > checkFactors() && checkTypes() && checkErrors() &&
            > required(this) ) ) {
            > return false;
            > }
            > }
            >
            > I also tried it without the 'this' just in case.[/color]

            Add some alerts in your functions to see where you are making it to so
            that you can find out where its stopping.
            [color=blue]
            > In the body:
            >
            > <FORM NAME="mainForm" onsubmit="retur n checkValues();"
            > ACTION="readfil e.php" method="POST">
            >
            > Thanks again for your help!
            >[/color]


            --
            Randy
            comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
            Answer:It destroys the order of the conversation
            Question: Why?
            Answer: Top-Posting.
            Question: Whats the most annoying thing on Usenet?

            Comment

            • Lee

              #7
              Re: New user- function doesn't work

              Randy Webb said:[color=blue]
              >
              >kdc wrote:[color=green]
              >> Thanks to both of you for your quick feedback.
              >> This is what I added to my program:
              >>
              >> function required(mainfo rm){
              >> var alertMessage = '';
              >> if (' '==mainform.sub 1.value ){alertMessage += "Name Required\n";}[/color]
              >
              >You are checking to see if sub1 is a space, not what you wanted.
              >[color=green]
              >> if (' '==mainform.sub 2.value){alertM essage += " Position Required\n";}[/color]
              >
              >Same there.
              >[color=green]
              >> if (''==mainform.s ub3.value){aler tMessage += "Email Required\n";}
              >> if (alertMessage == ''){return true;}
              >> else{alert(aler tMessage);retur n false;}
              >>
              >> }
              >>
              >>
              >> I wasn't sure whether to use =mainform or ==mainform so I also tried it
              >> with a single =. It continues to ignore it.[/color]
              >
              >It should be a double ==. My original had the typo in it and got
              >propogated by me copy/pasting it.
              >[color=green]
              >> I don't want to waste your time so let me ask if the fact that I call
              >> the function by
              >> adding it to another function (checkValues) that is then called in upon
              >> submission matters.[/color]
              >
              >Yes, it makes a huge difference.
              >[color=green]
              >> I added required(this) at the end of the list as below:[/color]
              >
              >"this", in that context, refers to the function, not the form. But since
              >you don't use the parameter, theres no point in adding it.[/color]

              The argument to required() *is* being used as a reference to the form.
              Changing "required(this) " to "required(docum ent.mainform)" looks like
              the easiest way to fix it.

              Comment

              • Thomas 'PointedEars' Lahn

                #8
                Re: New user- function doesn't work

                kdc wrote:
                [color=blue]
                > Thanks to both of you for your quick feedback.
                > This is what I added to my program:
                >
                > function required(mainfo rm){
                > var alertMessage = '';[/color]
                ^^^[1][color=blue]
                > if (' '==mainform.sub 1.value ){alertMessage += "Name Required\n";}[/color]
                ^^^ ^^^^^^^^^^^^^[4] ^^[3][color=blue]
                > if (' '==mainform.sub 2.value){alertM essage += " Position Required\n";}[/color]
                ^^^^^^^^^^^^^[4] ^^[3][color=blue]
                > if (''==mainform.s ub3.value){aler tMessage += "Email Required\n";}[/color]
                ^^^^^^^^^^^^^[4] ^^[3][color=blue]
                > if (alertMessage == ''){return true;}[/color]
                ^^^^^[3][color=blue]
                > else{alert(aler tMessage);retur n false;}
                >
                > }[/color]

                Which is not only badly formatted and thus hardly legible[1], but will
                also allow users to enter empty strings and strings consisting of two
                or more spaces or other whitespace in some input elements[2] while
                being inefficient[3] and not using standards compliant referencing[4].
                Therefore, it should instead read

                function required(mainfo rm)
                {
                var
                rxEmpty = new RegExp("^\\s*$" ),
                es = mainform.elemen ts,
                alertMessage = new Array();

                if (rxEmpty.test(e s["sub1"].value))
                {
                alertMessage.pu sh("Name Required");
                }

                if (rxEmpty.test(e s["sub2"].value))
                {
                alertMessage.pu sh("Position Required");
                }

                if (rxEmpty.test(e s["sub3"].value))
                {
                alertMessage.pu sh("Email Required");
                }

                return (alertMessage.l ength == 0 || !!alert(alertMe ssage.join("\n" ));
                }
                [color=blue]
                > I wasn't sure whether to use =mainform or ==mainform so I also tried it
                > with a single =. It continues to ignore it.[/color]

                It is not ignored but as you try to assign (`=') something to a (string)
                literal (' ' and ''), it causes a script error; with my UA:

                | SyntaxError: invalid assignment left-hand side

                But you have obviously either disabled the display of script errors or not
                had a look into the UA's JavaScript console.
                [color=blue]
                > I don't want to waste your time [...][/color]

                Great. Why do you not read then previous discussions on the subject before
                you post?


                PointedEars

                Comment

                Working...