[Javascript] Dynamic Form Validation

Teresa L Cannon Teresa.Cannon at ipaper.com
Wed Jul 18 08:15:09 CDT 2001


I'm trying to validate a form for a survey that has been dynamically
created by a web app.  This is
the ASP name of the form element: Name=""Q" & varQNumber(x) & "A" that
needs validating.

Is there a way in JavaScript to do this? If so, please be specific.
Needless to say, I am very new to
JavaScript & ASP.

Right now I would settle for "any type" of form validation. A generic alert
that would say "You must
answer all questions".  However, it would be nice to send them a message in
red, maybe even right before
the question to indicate which questions did not get answered.
__________________________________________________________________________

' db connection stuff & Dims go here..........
<HTML>
<HEAD>
<script language="JAVASCRIPT">
     function GoForm1()
     {
        var Str = document.Form1.DynamicFormNameHere.value")

          if (Str == null || inputStr == "0" || inputStr == "")
          {
               // the field is Null or blank.
               alert(" Please input an answer for all questions.")
               return false
          }
          else if (confirm(" Are you sure you want to Submit this Final
Survey"))
          {
               if (confirm(" Are you REALLY SURE?"))
                    {
                         document.Form1.submit()
                    }
          }
     }

</script>
</HEAD>
<body bgcolor="#F0F0DF" leftmargin="0" topmargin="10">

<form action="Survey-Script.asp" name="Form1" id="Form1" method="post">
<BR>
<TABLE>
      <%  varTotalAnswers = Request.QueryString("TotalAnswers")
          Response.Write(varTotalAnswers)

          rs.Source = _
               "SELECT DISTINCT QNumber, QTitle, ATypeID, QID FROM
EHS_SurveyQuestion " &_
               "WHERE SurveyID = '" & varSurveyID & "' " &_
               "ORDER By QNumber "
          rs.Open
          R = 0

          Do While NOT rs.EOF

               varQNumber(R) = rs(0)
               varQTitle(R) = rs(1)
               varATypeID(R) = rs(2)
               varQID(R) = rs(3)

               varCount = R
               R = R + 1
               rs.MoveNext
          Loop
          rs.Close

          ' This will loop through all of the questions for this survey up
to 61 questions and answers for each
question.

          For x = 0 to varCount

          Response.Write("<TR><TD></TD></TR><TR><TD>" & varQNumber(x) & ".
&nbsp;&nbsp;</TD><TD>")
                    Response.Write(varQTitle(x))
                    Response.Write("</TD></TR>")
                    Response.Write("<input type=""hidden"" Name=""QNumber"
& varQNumber(x) & """ value=""" & varQNumber(x)
& """>")

               rs.Source = _
               "SELECT DISTINCT ANumber, ATitle FROM EHS_SurveyAnswer " &_
               "WHERE SurveyID = '" & varSurveyID & "' " &_
               "AND QNumber = '" & varQNumber(x) & "' "
               rs.Open

               y = 0
               Do While NOT rs.EOF

                    varANumber(y) = rs(0)
                    varATitle(y) = rs(1)

                    varCountery = y
                    y = y + 1

                    rs.MoveNext
               Loop
               rs.close

               For z = 0 to varCountery

                    If varATypeID(x) = 1 OR varATypeID(x) = 2 Then
                         Response.Write("<TR><TD>&nbsp;</TD><TD>")
                         Response.Write("<input type=""" &
varType(varATypeID(x)) & """ Name=""Q" & varQNumber(x) & "A""
value=""" & varATitle(z) & """>")
                         Response.Write("&nbsp;" & varATitle(z))
                         Response.Write("</TD></TR>")
                         Response.Write("<input type=""hidden"" Name=""Q" &
varQNumber(x) & "A" & z & "B"" value=""" & varATitle(z)
& """>")
                    Else If varATypeID(x) = 4 Then
                              Response.Write("<input type=""" &
varType(varATypeID(x)) & """ Size=""65"" Name=""Q" & varQNumber(x)
& "A"">")
                         Response.Write("</TD></TR>")
                         Response.Write("<input type=""hidden"" Name=""Q" &
varQNumber(x) & "A" & z & "B"" value=""" & varATitle(z)
& """>")
                    Else If varATypeID(x) = 5 Then
                                   Response.Write("<TEXTAREA rows=2 cols=60
id=1 Name=""Q" & varQNumber(x) & "A""></TEXTAREA>")
                         Response.Write("</TD></TR>")
                         Response.Write("<input type=""hidden"" Name=""Q" &
varQNumber(x) & "A" & z & "B"" value=""" & varATitle(z)
& """>")
                    End If
                    End If
                    End If
               Next
          Next

     %>
</TABLE>
<TABLE>
     <TR>
          <TD>
          <input type="hidden" name="Survey" value="
<%Response.Write(varSurveyID)%>">
          <input type="hidden" name="LID" value="
<%Response.Write(varLID)%>">
          <input type="hidden" name="Count" value="
<%Response.Write(varCount)%>">
          <input TYPE="submit" VALUE="Submit Final Survey" Name="button1"
onClick="GoForm1()">
          </TD>
     </TR>
</TABLE>
</form>
</body>
</HTML>





More information about the Javascript mailing list