p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

proasp_howto thread: Dynamic validation with ASP and Javascript.


Message #1 by "David Yee" <david@r...> on Thu, 30 Aug 2001 19:56:19 +0800
Another possibility would be to do something similar to this (also 
added a
hidden input to let your handler know how many to process):

<SNIPPET>
<!-- FORM GENERATION -->
<% If CInt(Request.Form("Adult")) > 1 Then

	For i =3D 2 to CInt(Request.Form("Adult"))
            Response.Write "<tr>" & _
                               "<td>" & i & "</td>" & _
                               "<td><input type=3Dtext 
name=3DAdultname_" & i &
" maxlength=3D40 size=3D15></td>" & _
                               "<td><select name=3DAdultSex_" & i & ">" 
& _
                                                 "   <option
value=3DMale>Male</option>" & _
                                                 "   <option
value=3DFemale>Female</option>" & _
                                                 "</select></td>" & _
                               "<td><input type=3Dtext 
name=3DAdultpassport_" &
i & " size=3D9 maxlength=3D9></td>" & _
                               "<td><input type=3Dtext 
name=3DAdultexpiry_" & i
& " size=3D10 maxlength=3D10></td>" & _
                               "<td><input type=3Dtext 
name=3DAdultdob_" & i & "
size=3D10 maxlength=3D10></td>" & _
                               "<td><input type=3Dtext 
name=3DAdultnation_" & i
& " size=3D10 maxlength=3D40></td>" & _
                           "</tr>"
  	Next
   End If
%>
<INPUT TYPE=3D"HIDDEN" NAME=3D"ADULTCOUNT" VALUE=3D"<%
=3DCInt(Request.Form("Adult")) %>">



<!-- VALIDATION SCRIPT GENERATION -->
<SCRIPT Language=3D"JavaScript">
  function Verifyfield()
  {
     if (document.frm.First.value =3D=3D "")
     {
        alert("Required: Departure Date");
        document.frm.First.focus();
        return false;
     }
<% For i =3D 1 to CInt(Request.Form("adult")) %>
     if (document.frm.Adultexpiry_<% =3Di %>.value =3D=3D "")
     {
        alert("Required: Passport Expiry Date");
        document.frm.Adultexpiry_<% =3Di %>.focus();
        return false;
     }

     if (document.frm.Adultdob_<% =3Di %>.value =3D=3D "")
     {
        alert("Required: Passport Expiry Date");
        document.frm.Adultdob_<% =3Di %>.focus();
        return false;
     }
<% Next 'i %>
    
     return true;
  }
</SCRIPT>
</SNIPPET>


This method has worked well for me.  Hope it helps.

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
=3D=3D=3D=3D=3D=3D=3D=3D=3D
Kyle M. Burns, MCSD
ECommerce Technology Manager
Centra Credit Union
kburns@c...




-----Original Message-----
From: prasadk [mailto:prasadk@p...]
Sent: Thursday, August 30, 2001 7:36 AM
To: Pro ASP HowTo
Subject: [proasp_howto] Re: Dynamic validation with ASP and Javascript.


Hi,

You can do this in the following manner...just give a try..

I have added <%=3Di%> for each of the control name that you have used 
in the
for loop. This is to avoid similar names of controls for all the 
records. So
your ASP code looks like below:

ASP code to generate the form
--------------------------------

<% If CInt(Request.Form("Adult")) > 1 Then

  For i =3D 2 to CInt(Request.Form("Adult"))
            Response.Write "<tr>" & _
                               "<td>" & i & "</td>" & _
                               "<td><input type=3Dtext 
name=3DAdultname<%=3Di%>
maxlength@ size=15></td>" & _
                               "<td><select name=ADultSex<%=3Di%>
>" & _
                                                 "   <option
value=3DMale>Male</option>" & _
                                                 "   <option
value=FEmale>Female</option>" & _
                                                 "</select></td>" & _
                               "<td><input type=3Dtext
name=ADultpassport<%=3Di%>

size=3D9 maxlength=3D9></td>" & _
                               "<td><input type=3Dtext 
name=3DAdultexpiry<%=3Di%>

size=10 maxlength=10></td>" & _
                               "<td><input type=3Dtext 
name=3DAdultdob<%=3Di%>
 size=10
maxlength=10></td>" & _
                               "<td><input type=3Dtext 
name=3DAdultnation<%=3Di%>
size=10 maxlength@></td>" & _
                           "</tr>"
  Next
End If %>
'-----------------------------------------------------------------------
----
------------------------

'Now coming to Java script validation for each of the control.....

'-----------------------------------------------------------------------
----
------------------------
<SCRIPT Language=3D"JavaScript">
  function Verifyfield(){

     if (document.frm.First.value =3D=3D "") {
    alert("Required: Departure Date");
    document.frm.First.focus();
    return false
   }

for (j =3D 2; j < document.frm.elements.length; j++)
{
    if ((document.frm.elements[j].name.substring(0,11)) =3D=3D 
'Adultexpiry')
    {
        if (document.frm.elements[j].value =3D=3D "")
        {
            alert("Required: Passport Expiry Date");
            document.frm.elements[j].focus();
            return false
        }
    }
    elseif ((document.frm.elements[j].name.substring(0,8)) =3D=3D 
'Adultdob')


        if (document.frm.elements[j].value =3D=3D "")
        {
            alert("Required: Passport Expiry Date");
            document.frm.elements[j].focus();
            return false;
        }
    }
   else
       return true;
    }
}
</script>


I think this will do for you.

Prasad.


  Return to Index