[Javascript] Dynamic Form Validation

Josiah Gordon jgordon at directfile.com
Thu Jul 19 12:05:39 CDT 2001


Teresa,

I have written a script that will allow you to validate dynamic content
while being blind to the actual page that is displayed. I do this by passing
a parameter called "dataType" [my variable name] from the server into the
onChange property and a reference to the object itself that needs
validation, like this:

<input type="text" size="45" name="phyzip" value="" maxlength="10"
onBlur="update(&#34;zip&#34;, this);">

I then sum up the fields, and the references to the objects themselves, that
erred  on validation into a queue. When onSubmit is fired I check the error
queue for the error messages. If none, the form submits. Otherwise, the user
must correct the bad fields.

You could adapt this method for checking if all fields are filled out by
looping through the array of elements in the document. If you *just* need to
"indicate which questions did not get answered" I would suggest this
approach.

Let me know if you need any more pointers.



--Josiah Gordon



-----Original Message-----
From: javascript-admin at LaTech.edu [mailto:javascript-admin at LaTech.edu]On
Behalf Of Teresa L Cannon
Sent: Wednesday, July 18, 2001 6:15 AM
To: javascript at LaTech.edu
Subject: [Javascript] Dynamic Form Validation


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>


_______________________________________________
Javascript mailing list
Javascript at LaTech.edu
http://www.LaTech.edu/mailman/listinfo/javascript


---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.264 / Virus Database: 136 - Release Date: 7/2/2001

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.265 / Virus Database: 137 - Release Date: 7/18/2001




More information about the Javascript mailing list