Form validation object expected

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

    #1

    Form validation object expected

    Our regular programmer moved on. I'm almost clueless in Javascript/ASP and
    got the job of adapting existing code. In the page below, everything works
    until I added the function checkIt() to validate which radio button was
    clicked and what was in a textfield. The form is an attendance checking page
    for a cyber charter school. What I'm trying to accomplish is that if a
    parent marks the student present, there should not be anything in the
    reasonforabsenc e field. If the parent marks the student absent, there should
    be something present in the reasonforabsenc e field.

    What happens nows is when the Submit button is clicked an error is generated
    on the page in IE. The error says Object Expected in the first if statement
    of the checkIt() function and I have no idea what it's asking for.

    What am I missing and/or is there a better way of accomplishing the
    validation?

    Thanks much,
    Steve




    <!--#include file="security_ parent.asp"-->



    <%

    ' initialize variables

    sid = request.queryst ring("sid")

    themonth = request.queryst ring("themonth" )

    theday = request.queryst ring("theday")

    theyear = request.queryst ring("theyear")

    %>



    <!--#include file="connectio n_open.asp"-->



    <%

    set rsstudent = conn.execute("S ELECT * from studentinfo where sid = " & sid
    & "")

    bartext = "CPDLF Attendance Log - " & rsstudent("fnam e")

    rsstudent.close

    set rsstudent = nothing



    set rs = conn.execute("S ELECT * FROM studenttocourse where sid = " & sid &
    "")

    %>

    <html>

    <head>

    <title>Centra l Pennsylvania Digital Learning Foundation</title>

    <script language="JavaS cript">



    function textLimit(field , maxlen) {

    if (field.value.le ngth > maxlen) {

    field.value = field.value.sub string(0, maxlen);

    } }





    function replaceChars(en try) {

    out = "'"; // replace this

    add = "`"; // with this

    temp = "" + entry; // temporary holder



    while (temp.indexOf(o ut)>-1) {

    pos= temp.indexOf(ou t);

    temp = "" + (temp.substring (0, pos) + add +

    temp.substring( (pos + out.length), temp.length));

    }

    document.attend ance.reasonfora bsence.value = temp;

    }



    function checkIt() {

    var AttendedChecked = false;

    var AbsentChecked = false;

    if(document.att end.attendance[0].checked) {

    AttendedChecked = true;

    if(document.att end.reasonforab sence.value.len gth=0) {

    document.attend .submit();

    }else{

    alert("You have marked the student present, but given a reason
    for absence.");

    }

    }

    if(document.att end.attendance[1].checked) {

    AbsentChecked = true;

    if(document.att end.reasonforab sence.value.len gth>0) {

    document.attend .submit();

    }else{

    alert("Please enter a valid reason for absence and try again.");

    }

    }



    </script>

    </head>

    <body bgcolor="white"
    onLoad="MM_prel oadImages('cybe rhome_files/homebar.gif','c yberhome_files/info
    bar.jpg')">



    <table border=0 align="center" width=600>

    <!--#include file="header_ge neric.asp"-->

    <tr>

    <td align="Center" colspan=2><br>

    <font face="Arial, Helvetica, sans-serif" size="2">Please click the
    appropriate response below to indicate whether your child was present or
    absent for the listed day.

    If your child is absent for the day, list the reason for the
    absence in the space provided and press the

    "Submit" button below.<br><br>

    You are currently logging attendance for <%= themonth %>/<%= theday %>/<%=
    theyear %>.</font>

    <form method="post" action="updatea ttendancelog.as p" name="attend">

    <input type="hidden" name="sid" value="<%= sid %>">

    <table width="335">

    <tr><td colspan="2"><in put type="Radio" name="attendanc e"
    value="Attended " checked><font face="Arial, Helvetica, sans-serif"
    size="2">This student was present on <%= themonth %>/<%= theday %>/<%=
    theyear %>.</font></td></tr>

    <tr><td colspan="2"><in put type="Radio" name="attendanc e"
    value="Absent"> <font face="Arial, Helvetica, sans-serif" size="2">This
    student was absent on <%= themonth %>/<%= theday %>/<%= theyear
    %>.</font></td></tr>

    <tr><td colspan="2"><fo nt face="Arial, Helvetica, sans-serif"
    size="2"><b>Rea son for absence</b></font> <font face="Arial, Helvetica,
    sans-serif" size="1">(maxim um 100 characters)</font></td></tr>

    <tr><td colspan="2"><te xtarea name="reasonfor absence" cols="40"
    rows="3" wrap="PHYSICAL" onkeyup="textLi mit(this.form.r easonforabsence ,
    100);"></textarea>

    <br></td></tr>

    <tr>

    <td align="center" colspan=2>

    <input type="hidden" name="themonth" value="<%= themonth %>">

    <input type="hidden" name="theday" value="<%= theday %>">

    <input type="hidden" name="theyear" value="<%= theyear %>">

    <input type="button" value="Submit"
    onClick="checkI t()";"replaceCh ars(document.at tendance.reason forabsence.valu e
    )"><br>

    <br>

    <font face="Arial, Helvetica, sans-serif" size="2"><a
    href="AttendInf olist.asp?sid=< %= sid %>">Click here to view this child's
    logged attendance for this school year (summary view)</a>.</font><br>



    <br>

    <font face="Arial, Helvetica, sans-serif" size="2">If you are experiencing
    technical difficulties with this process, please

    <a href="mailto:te chsupport@cpdlf .org">notify our Tech Support</a>.
    Include the

    specific error message you're getting in the email.</font><br>

    </td>

    </tr>

    </table>

    </form>

    <br>

    <font face="Arial, Helvetica, sans-serif" size="2">

    <p align="center"> <a href="parentmen u.asp">Back</a></p>

    </font>

    </tr>

    </table>

    </body>

    </html>


  • Mick White

    #2
    Re: Form validation object expected

    Steve Benson wrote:

    [snip][color=blue]
    >
    > What am I missing and/or is there a better way of accomplishing the
    > validation?
    >
    > Thanks much,
    > Steve[/color]

    [color=blue]
    >
    > <script language="JavaS cript">
    > function textLimit(field , maxlen) {
    > if (field.value.le ngth > maxlen) {
    > field.value = field.value.sub string(0, maxlen);
    >} }[/color]
    [color=blue]
    > function replaceChars(en try) {
    > out = "'"; // replace this
    > add = "`"; // with this
    > temp = "" + entry; // temporary holder
    > while (temp.indexOf(o ut)>-1) {
    > pos= temp.indexOf(ou t);
    > temp = "" + (temp.substring (0, pos) + add +
    > temp.substring( (pos + out.length), temp.length));
    > }
    > document.attend ance.reasonfora bsence.value = temp;>
    > }
    >[/color]

    The replaceChars function lends itself to the use of regular expressions:

    function replaceChars(en try) {
    document.attend ance.reasonfora bsence.value=en try.replace(/'/g,"`")
    }[color=blue]
    >
    >
    > function checkIt() {
    >
    > var AttendedChecked = false;
    >
    > var AbsentChecked = false;
    >
    > if(document.att end.attendance[0].checked) {
    >
    > AttendedChecked = true;[/color]

    [color=blue]
    >
    > if(document.att end.reasonforab sence.value.len gth=0) {[/color]

    if(document.att end.reasonforab sence.value.len gth==0)
    //Note the double equals
    or
    if(!document.at tend.reasonfora bsence.value)

    Mick[color=blue]
    >
    > document.attend .submit();
    >
    > }else{
    >
    > alert("You have marked the student present, but given a reason
    > for absence.");
    >
    > }
    >
    > }
    >
    > if(document.att end.attendance[1].checked) {
    >
    > AbsentChecked = true;
    >
    > if(document.att end.reasonforab sence.value.len gth>0) {
    >
    > document.attend .submit();
    >
    > }else{
    >
    > alert("Please enter a valid reason for absence and try again.");
    >
    > }
    >
    > }
    >
    >
    >
    > </script>[/color]

    Comment

    • Michael Winter

      #3
      Re: Form validation object expected

      On Mon, 30 Aug 2004 10:06:31 -0400, Steve Benson
      <sbenson@nospam please.alt3.com > wrote:

      [snip]

      I'm going to comment on various parts of your post, in order. I will
      address the actual issue, just bear with me. :)
      [color=blue]
      > <%
      >
      > ' initialize variables
      >
      > sid = request.queryst ring("sid")
      >
      > themonth = request.queryst ring("themonth" )
      >
      > theday = request.queryst ring("theday")
      >
      > theyear = request.queryst ring("theyear")
      >
      > %>[/color]

      Please don't post ASP code to this group. Instead, show the client-side
      results.

      [snip]

      Valid HTML documents should include a DTD. See:

      <URL:http://www.w3.org/TR/html4/struct/global.html#h-7.2>
      [color=blue]
      > <html>[/color]

      [snip]
      [color=blue]
      > <script language="JavaS cript">[/color]

      The language attribute has been deprecated in favour of the (required)
      type attribute. The former should not be used any more.

      <script type="text/javascript">

      [snip]
      [color=blue]
      > function replaceChars(en try) {
      > out = "'"; // replace this
      > add = "`"; // with this
      > temp = "" + entry; // temporary holder[/color]

      All of these should be declared with the var keyword to prevent them from
      becoming global. However, I present a much simpler approach later.

      [snip]
      [color=blue]
      > function checkIt() {[/color]

      This function should be rewritten to validate upon submission of the form,
      not clicking a button. By failing to use a proper submit button, you make
      your page needlessly dependant on Javascript.

      function checkIt(form) {
      if(form.element s['attendance'][0].checked) {
      if(!form.elemen ts['reasonforabsen ce'].value.length) {
      return true;
      } else {
      alert('You have marked the student present, but given a '
      + 'reason for absence.');
      }
      } else if(form.element s['attendance'][1].checked) {
      if(form.element s['reasonforabsen ce'].value.length) {
      return true;
      } else {
      if(!form.elemen ts['reasonforabsen ce'].value.length) {
      return true;
      } else {
      alert('Please enter a valid reason for absence.');
      }
      }
      } else {
      // Just in case.
      alert('Please state whether the student was present or absent.);
      }
      return false;
      }

      Then, on the form:

      <form ... onsubmit="retur n checkIt(this);" >
      [color=blue]
      > var AttendedChecked = false;
      > var AbsentChecked = false;[/color]

      As you don't use these booleans, I fail to see the point of their
      inclusion.
      [color=blue]
      > if(document.att end.attendance[0].checked) {
      >
      > AttendedChecked = true;
      >
      > if(document.att end.reasonforab sence.value.len gth=0) {[/color]

      Here, you're not actually performing a comparison.
      [color=blue]
      > document.attend .submit();[/color]

      You should rarely, if ever, need to call the submit method.

      [snip]
      [color=blue]
      > <table width="335">[/color]

      It tends to be a bad idea fixing element size in pixels. It means that the
      element can't expand if font sizes differ from those intended.
      [color=blue]
      > <tr><td colspan="2"><in put type="Radio" name="attendanc e"
      > value="Attended " checked><font face="Arial, Helvetica, sans-serif"
      > size="2">This student was present on <%= themonth %>/<%= theday
      > %>/<%= theyear %>.</font></td></tr>[/color]
      [color=blue]
      > <tr><td colspan="2"><in put type="Radio" name="attendanc e"
      > value="Absent"> <font face="Arial, Helvetica, sans-serif"
      > size="2">This student was absent on <%= themonth %>/<%= theday
      > %>/<%= theyear %>.</font></td></tr>
      >
      > <tr><td colspan="2"><fo nt face="Arial, Helvetica, sans-serif"
      > size="2"><b>Rea son for absence</b></font> <font
      > face="Arial, Helvetica, sans-serif" size="1">
      > (maximum 100 characters)</font></td></tr>[/color]

      Please consider upgrading this site to use CSS. Trying to read through
      that mess gives me a headache. It also fills a page with redundant,
      deprecated FONT elements that could be replaced by

      body {
      font-family: Arial, Helvetica, sans-serif;
      font-size: 100%;
      }
      [color=blue]
      > <tr><td colspan="2"><te xtarea name="reasonfor absence"
      > cols="40"
      > rows="3" wrap="PHYSICAL"
      > onkeyup="textLi mit(this.form.r easonforabsence , 100);"></textarea>[/color]

      It would probably be nicer for the user for this restriction to be
      mentioned on the page and validated upon submission, rather than being
      enforced during entry. Whilst you're at it, call replaceChars() here,
      using the change event. It's much more logical.

      <textarea ... onchange="repla ceChars(this);" >

      function replaceChars(en try) {
      entry.value = entry.value.rep lace(/\'/g, '`');
      }

      Don't forget to do this, and other validation, on the server, too.

      [snip]
      [color=blue]
      > <input type="button" value="Submit"
      > onClick="checkI t()";"replaceCh ars(document.at tendance.reason forabsence.valu e
      > )"><br>[/color]

      There are two issues here, more if you don't move the replaceChars()
      function as proposed:

      1) If you want a submit button, use a submit button. That gives people
      without Javascript a chance to use your page.
      2) There's a syntax error with your onclick attribute.

      However, the latter is irrelevant as you should be moving both processes
      to other elements.

      <input type="submit" value="Submit">

      [snip]
      [color=blue]
      > <a href="mailto:te chsupport@cpdlf .org">notify our Tech Support</a>.[/color]

      Mailto: is generally a bad idea. Use form-mail instead.

      [snip]

      Hope that helps,
      Mike

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

      Comment

      • Lee

        #4
        Re: Form validation object expected

        Steve Benson said:
        [color=blue]
        >What am I missing and/or is there a better way of accomplishing the
        >validation?[/color]
        [color=blue]
        >function checkIt() {
        >
        > var AttendedChecked = false;
        >
        > var AbsentChecked = false;[/color]

        You don't need these variables. You never use their values.

        [color=blue]
        >if(document.at tend.attendance[0].checked) {
        >
        > AttendedChecked = true;
        >
        >if(document.at tend.reasonfora bsence.value.le ngth=0) {[/color]


        The test for equality is "==", not "=".
        [color=blue]
        > document.attend .submit();[/color]


        Submitting the form from this function causes two problems.
        1. People who don't have Javascript enabled will not be able
        to submit the form at all.
        2. The function replaceChars() will never be called, because
        the form is submitted before you invoke it:

        [color=blue]
        > <input type="button" value="Submit"
        >onClick="check It()";"replaceC hars(document.a ttendance.reaso nforabsence.val ue
        >)">[/color]

        I also notice that your replaceChars() invocation refers to
        "document.atten dance", instead of "document.atten d".

        It would be better to use an input of type "submit", and to do your validation
        in the form's onsubmit handler. If your validation function finds problems, you
        can return false from the onsubmit handler to prevent submission.

        <form name="attend"
        method="post"
        onsubmit="repla ceChars(this.re asonforabsence. value);return checkIt()">

        Then modify checkIt() so that it returns true if the form should be submitted,
        and false if it should not.

        Comment

        • Steve Benson

          #5
          Re: Form validation object expected

          Thanks for the help, Mike. I made almost all of the changes you advised (no
          style sheet or mailto changes). I now get no errors when the page loads and
          no errors after clicking submit, but no validation either. Form submits to
          database regardless of radio buttons clicked or textarea status.

          I think I put all of the important pieces in as you wrote. Sorry to be such
          a noob, but did I paste something in incorrectly to cause the validation not
          to fire?

          Thanks again for your help,
          Steve

          Here's the code, minus ASP bits:

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
          "http://www.w3.org/TR/html4/loose.dtd">
          <html>
          <head>
          <title>Centra l Pennsylvania Digital Learning Foundation</title>
          <script type="text/javaScript">

          function checkIt(form) {
          if(form.element s['attendance'][0].checked) {
          if(!form.elemen ts['reasonforabsen ce'].value.length) {
          return true;
          } else {
          alert('You have marked the student present, but given a '
          + 'reason for absence.');
          }
          } else if(form.element s['attendance'][1].checked) {
          if(form.element s['reasonforabsen ce'].value.length) {
          return true;
          } else {
          if(!form.elemen ts['reasonforabsen ce'].value.length) {
          return true;
          } else {
          alert('Please enter a valid reason for absence.');
          }
          }
          } else {
          // Just in case.
          alert('Please state whether the student was present or absent.');
          }
          return false;
          }

          function replaceChars(en try) {
          entry.value = entry.value.rep lace(/\'/g, '`');
          }

          function textLimit(field , maxlen) {
          if (field.value.le ngth > maxlen) {
          field.value = field.value.sub string(0, maxlen);
          } }

          </script>
          </head>
          <body bgcolor="white" >

          <table border=0 align="center" width=600>

          <tr>
          <td align="left">
          <a href="index.asp ">
          <img src="cyberhome_ files/dlf%20logo.gif" border="0" width="275"
          height="96">
          </a>
          </td>
          <td align="right" valign="bottom" >&nbsp; </td>
          </tr>
          <tr>
          <td colspan=2 bgcolor="#91A1C A" align="center">
          <font face="Arial" color="white">
          <b>
          CPDLF Attendance Log - Joshua
          </b>
          </font>
          </td>
          </tr>
          <tr>
          <td align="Center" colspan=2><br>

          <font face="Arial, Helvetica, sans-serif" size="2">Please click the
          appropriate response below to indicate whether your child was present or
          absent for the listed day.
          If your child is absent for the day, list the reason for the absence in
          the space provided and press the
          "Submit" button below.<br><br>
          You are currently logging attendance for 9/1/2004.</font>

          <form method="post" action="updatea ttendancelog.as p" name="attend"
          onsubmit="retur n checkIt(this);" >
          <input type="hidden" name="sid" value="957">
          <table width="335">
          <tr><td colspan="2"><in put type="Radio" name="attendanc e" value="Attended "
          checked>
          <font face="Arial, Helvetica, sans-serif" size="2">This student was present
          on 9/1/2004.</font></td></tr>
          <tr><td colspan="2"><in put type="Radio" name="attendanc e" value="Absent">
          <font face="Arial, Helvetica, sans-serif" size="2">This student was absent
          on 9/1/2004.</font></td></tr>
          <tr><td colspan="2"><fo nt face="Arial, Helvetica, sans-serif"
          size="2"><b>Rea son for absence</b></font>
          <font face="Arial, Helvetica, sans-serif" size="1">(maxim um 100
          characters)</font></td></tr>
          <tr><td colspan="2"><te xtarea name="reasonfor absence" cols="40" rows="3"
          wrap="PHYSICAL"
          onchange="repla ceChars(this);"
          onkeyup="textLi mit(this.form.r easonforabsence , 100);"></textarea>
          <br></td></tr>
          <tr>
          <td align="center" colspan=2>
          <input type="hidden" name="themonth" value="9">
          <input type="hidden" name="theday" value="1">
          <input type="hidden" name="theyear" value="2004">
          <input type="Submit" value="Submit"> <br>
          <br>
          <font face="Arial, Helvetica, sans-serif" size="2"><a
          href="AttendInf olist.asp?sid=9 57">Click here to view this child's logged
          attendance for this school year (summary view)</a>.</font><br>

          <br>
          <font face="Arial, Helvetica, sans-serif" size="2">If you are
          experiencing technical difficulties with this process, please
          <a href="mailto:te chsupport@cpdlf .org">notify our Tech Support</a>.
          Include the
          specific error message you're getting in the email.</font><br>
          </td>
          </tr>
          </table>
          </form>
          <br>
          <font face="Arial, Helvetica, sans-serif" size="2">
          <p align="center"> <a href="parentmen u.asp">Back</a></p>
          </font>
          </tr>
          <script language="JavaS cript">
          <!--
          function MM_preloadImage s() { //v3.0
          var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
          var i,j=d.MM_p.leng th,a=MM_preload Images.argument s; for(i=0; i<a.length;
          i++)
          if (a[i].indexOf("#")!= 0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
          }

          function MM_swapImgResto re() { //v3.0
          var i,x,a=document. MM_sr; for(i=0;a&&i<a. length&&(x=a[i])&&x.oSrc;i++ )
          x.src=x.oSrc;
          }

          function MM_findObj(n, d) { //v3.0
          var p,i,x; if(!d) d=document;
          if((p=n.indexOf ("?"))>0&&paren t.frames.length ) {
          d=parent.frames[n.substring(p+1 )].document; n=n.substring(0 ,p);}
          if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.fo rms.length;i++)
          x=d.forms[i][n];
          for(i=0;!x&&d.l ayers&&i<d.laye rs.length;i++)
          x=MM_findObj(n, d.layers[i].document); return x;
          }

          function MM_swapImage() { //v3.0
          var i,j=0,x,a=MM_sw apImage.argumen ts; document.MM_sr= new Array;
          for(i=0;i<(a.le ngth-2);i+=3)
          if ((x=MM_findObj( a[i]))!=null){docum ent.MM_sr[j++]=x; if(!x.oSrc)
          x.oSrc=x.src; x.src=a[i+2];}
          }
          //-->
          </script>
          <tr>
          <td align="center" colspan=2><img height=38
          src="cyberhome_ files/infobar.jpg" width=523 name="bar"></td>
          </tr>

          <tr>
          <td align="center" colspan=2><a href="mission.a sp"
          onMouseOut="MM_ swapImage('Imag e15','','cyberh ome_files/icon6.jpg',1);M M_swap
          Image('bar','', 'cyberhome_file s/infobar.jpg',1) "
          onMouseOver="MM _swapImage('Ima ge15','','cyber home_files/icon1a.jpg',1); MM_sw
          apImage('bar',' ','cyberhome_fi les/missionbar.gif' ,1)"><img border=0
          height=40 name=Image15 src="cyberhome_ files/icon6.jpg" width=36></a><a
          href="academics .asp"
          onMouseOut="MM_ swapImage('Imag e16','','cyberh ome_files/icon5.jpg',1);M M_swap
          Image('bar','', 'cyberhome_file s/infobar.jpg',1) "
          onMouseOver="MM _swapImage('Ima ge16','','cyber home_files/icon2a.jpg',1); MM_sw
          apImage('bar',' ','cyberhome_fi les/acadmcbar.gif', 1)"><img border=0 height=40
          name=Image16 src="cyberhome_ files/icon5.jpg" width=36></a><a href="why.asp"
          onMouseOut="MM_ swapImage('Imag e17','','cyberh ome_files/icon4.jpg',1);M M_swap
          Image('bar','', 'cyberhome_file s/infobar.jpg',1) "
          onMouseOver="MM _swapImage('Ima ge17','','cyber home_files/icon3a.jpg',1); MM_sw
          apImage('bar',' ','cyberhome_fi les/whybar.gif',1)" ><img border=0 height=40
          name=Image17 src="cyberhome_ files/icon4.jpg" width=36></a><a
          href="applicati on.asp"
          onMouseOut="MM_ swapImage('Imag e18','','cyberh ome_files/icon3.jpg',1);M M_swap
          Image('bar','', 'cyberhome_file s/infobar.jpg',1) "
          onMouseOver="MM _swapImage('Ima ge18','','cyber home_files/icon4a.jpg',1); MM_sw
          apImage('bar',' ','cyberhome_fi les/applicatnbar.gi f',1)"><img border=0
          height=40 name=Image18 src="cyberhome_ files/icon3.jpg" width=36></a><a
          href="partnersh ips.asp"
          onMouseOut="MM_ swapImage('Imag e19','','cyberh ome_files/icon2.jpg',1);M M_swap
          Image('bar','', 'cyberhome_file s/infobar.jpg',1) "
          onMouseOver="MM _swapImage('Ima ge19','','cyber home_files/icon5a.jpg',1); MM_sw
          apImage('bar',' ','cyberhome_fi les/partnrbar.gif', 1)"><img border=0 height=40
          name=Image19 src="cyberhome_ files/icon2.jpg" width=36></a><a href="info.asp"
          onMouseOut="MM_ swapImage('Imag e20','','cyberh ome_files/icon1.jpg',1);M M_swap
          Image('bar','', 'cyberhome_file s/infobar.jpg',1) "
          onMouseOver="MM _swapImage('Ima ge20','','cyber home_files/icon6a.jpg',1); MM_sw
          apImage('bar',' ','cyberhome_fi les/informatnbar.gi f',1)"><img border=0
          height=40 name=Image20 src="cyberhome_ files/icon1.jpg" width=36></a><p
          align="Center"> <font size="1">© 2003, CPDLF</font></p></td>
          </tr>
          </table>
          </body>
          </html>


          Comment

          • Michael Winter

            #6
            Re: Form validation object expected

            On Mon, 30 Aug 2004 14:38:10 -0400, Steve Benson
            <sbenson@nospam please.alt3.com > wrote:

            [snip]
            [color=blue]
            > I think I put all of the important pieces in as you wrote. Sorry to be
            > such a noob, but did I paste something in incorrectly to cause the
            > validation not to fire?[/color]

            No, it's my fault.

            [snip]
            [color=blue]
            > function checkIt(form) {
            > if(form.element s['attendance'][0].checked) {
            > if(!form.elemen ts['reasonforabsen ce'].value.length) {
            > return true;
            > } else {
            > alert('You have marked the student present, but given a '
            > + 'reason for absence.');
            > }
            > } else if(form.element s['attendance'][1].checked) {
            > if(form.element s['reasonforabsen ce'].value.length) {
            > return true;
            > } else {[/color]

            The following section shouldn't exist. I think I copy and pasted once too
            often.
            [color=blue]
            > if(!form.elemen ts['reasonforabsen ce'].value.length) {
            > return true;
            > } else {
            > alert('Please enter a valid reason for absence.');
            > }[/color]

            Replace it with:

            alert('Please enter a valid reason for absence.');
            [color=blue]
            > }
            > } else {
            > // Just in case.
            > alert('Please state whether the student was present or absent.');
            > }
            > return false;
            > }[/color]

            By the way, a simple test which you might want to consider in future is to
            place alert() calls in various places within the code. That can help you
            see what's being executed, and what's not.

            [snipped remaining code]

            If there's still a problem (I don't think there will be, but test
            thoroughly to be sure), don't hesitate to say.

            Sorry for the mistake.

            Mike

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

            Comment

            • Steve Benson

              #7
              Re: Form validation object expected

              Still not quite there. I tried it with your revised version and then another
              shot-in-the-dark revision of my own. Neither way triggers the validation,
              but just plow ahead to the next page. I made no changes to the form itself.

              Thanks again for your assistance,

              Steve

              I tried it this way (your revised):

              function checkIt(form) {

              if(form.element s['attendance'][0].checked) {

              if(!form.elemen ts['reasonforabsen ce'].value.length) {

              return true;

              } else {

              alert('You have marked the student present, but given a '

              + 'reason for absence.');

              }

              } else if(form.element s['attendance'][1].checked) {

              if(form.element s['reasonforabsen ce'].value.length) {

              return true;

              } else {

              alert('Please enter a valid reason for absence.');

              }

              }

              } else {

              // Just in case.

              alert('Please state whether the student was present or absent.');

              }

              return false;

              }

              and this way (my shot-in-the-dark revision):

              function checkIt(form) {

              if(form.element s['attendance'][0].checked) {

              if(form.element s['reasonforabsen ce'].value.length== 0) {

              return true;

              } else {

              alert('You have marked the student present, but given a '

              + 'reason for absence.');

              }

              } else if(form.element s['attendance'][1].checked) {

              if(form.element s['reasonforabsen ce'].value.length>0 ) {

              return true;

              } else {

              alert('Please enter a valid reason for absence.');

              }

              }

              } else {

              // Just in case.

              alert('Please state whether the student was present or absent.');

              }

              return false;

              }


              Comment

              • Michael Winter

                #8
                Re: Form validation object expected

                On Mon, 30 Aug 2004 15:51:42 -0400, Steve Benson
                <sbenson@nospam please.alt3.com > wrote:

                [snip]

                The code you presented should cause a syntax error. I've reformatted it,
                and you should see that the braces don't line up:

                function checkIt(form) {
                if(form.element s['attendance'][0].checked) {
                if(!form.elemen ts['reasonforabsen ce'].value.length) {
                return true;
                } else {
                alert('You have marked the student present, but given a '
                + 'reason for absence.');
                }
                } else if(form.element s['attendance'][1].checked) {
                if(form.element s['reasonforabsen ce'].value.length) {
                return true;
                } else {
                alert('Please enter a valid reason for absence.');
                }
                } // Remove this brace and shift the lower lines accordingly
                } else {
                alert('Please state whether the student was present or absent.');
                }
                return false;
                }

                Remove the marked brace and all should be well (I found no problems).

                [snipped shot-in-the-dark]

                Hope that helps,
                Mike

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

                Comment

                • Steve Benson

                  #9
                  Re: Form validation object expected

                  That did the trick! Thank you SO much for your help. I'd been
                  wrestling with that for literally days.

                  Thanks again,
                  Steve

                  Comment

                  • Steve Benson

                    #10
                    Re: Form validation object expected

                    Thanks for the help. Much appreciated!

                    Steve


                    On Mon, 30 Aug 2004 14:28:53 GMT, Mick White
                    <mwhite13@BOGUS rochester.rr.co m> wrote:
                    [color=blue]
                    >Steve Benson wrote:
                    >
                    >[snip][color=green]
                    >>
                    >> What am I missing and/or is there a better way of accomplishing the
                    >> validation?
                    >>
                    >> Thanks much,
                    >> Steve[/color]
                    >
                    >[color=green]
                    >>
                    >> <script language="JavaS cript">
                    >> function textLimit(field , maxlen) {
                    >> if (field.value.le ngth > maxlen) {
                    >> field.value = field.value.sub string(0, maxlen);
                    >>} }[/color]
                    >[color=green]
                    >> function replaceChars(en try) {
                    >> out = "'"; // replace this
                    >> add = "`"; // with this
                    >> temp = "" + entry; // temporary holder
                    >> while (temp.indexOf(o ut)>-1) {
                    >> pos= temp.indexOf(ou t);
                    >> temp = "" + (temp.substring (0, pos) + add +
                    >> temp.substring( (pos + out.length), temp.length));
                    >> }
                    >> document.attend ance.reasonfora bsence.value = temp;>
                    >> }
                    >>[/color]
                    >
                    >The replaceChars function lends itself to the use of regular expressions:
                    >
                    >function replaceChars(en try) {
                    >document.atten dance.reasonfor absence.value=e ntry.replace(/'/g,"`")
                    >}[color=green]
                    >>
                    >>
                    >> function checkIt() {
                    >>
                    >> var AttendedChecked = false;
                    >>
                    >> var AbsentChecked = false;
                    >>
                    >> if(document.att end.attendance[0].checked) {
                    >>
                    >> AttendedChecked = true;[/color]
                    >
                    >[color=green]
                    >>
                    >> if(document.att end.reasonforab sence.value.len gth=0) {[/color]
                    >
                    >if(document.at tend.reasonfora bsence.value.le ngth==0)
                    >//Note the double equals
                    >or
                    >if(!document.a ttend.reasonfor absence.value)
                    >
                    >Mick[color=green]
                    >>
                    >> document.attend .submit();
                    >>
                    >> }else{
                    >>
                    >> alert("You have marked the student present, but given a reason
                    >> for absence.");
                    >>
                    >> }
                    >>
                    >> }
                    >>
                    >> if(document.att end.attendance[1].checked) {
                    >>
                    >> AbsentChecked = true;
                    >>
                    >> if(document.att end.reasonforab sence.value.len gth>0) {
                    >>
                    >> document.attend .submit();
                    >>
                    >> }else{
                    >>
                    >> alert("Please enter a valid reason for absence and try again.");
                    >>
                    >> }
                    >>
                    >> }
                    >>
                    >>
                    >>
                    >> </script>[/color][/color]

                    Comment

                    • Steve Benson

                      #11
                      Re: Form validation object expected

                      Thanks for the help. Much appreciated.

                      Steve

                      Comment

                      Working...