www.webdeveloper.com
Results 1 to 8 of 8

Thread: Validate Checkboxes

  1. #1
    Join Date
    Jan 2005
    Posts
    80

    Wink Validate Checkboxes

    i like to validate the checkboxes in my form but how should i create an array or a loop? the value for my checkboxes is the name of the product. i have placed it in a loop. the following is my code.

    Code:
    If Request("Submit")="Submit" then
    
    v = request.form("value")
    ex = request.form ("exp")
    unit = request.form("unit")
    
    sql1 =  "Select * from ABS where item = '" &i& "'AND unit = '" &unit& "'"
    Set Rs1 = Server.CreateObject("ADODB.RecordSet")
    Rs1.open sql1,conn
    If Rs1.eof then
    Response.write "No records found,please try again!!!"
    
    Else
    	if ex = "greater or equals to" then
    	
    	sqlC =  "Select SubID,Item, Grade, GradeName from ABSSubGrade where Grade >= " &v& " AND ABSID = "&rs1("ABSID")&" "
    
    	elseif ex = "equals to" then
    	sqlC =  "Select SubID,Item, Grade, GradeName from ABSSubGrade where Grade = " &v& " AND ABSID = "&rs1("ABSID")&" "
    	
    	else
    	sqlC =  "Select SubID,Item, Grade, GradeName from ABSSubGrade where Grade <= " &v& " AND ABSID = "&rs1("ABSID")&" "
    	
    	end if
    	
    Set RSC = Server.CreateObject("ADODB.RecordSet")
    RSC.open sqlC,conn
    if RSC.eof then
    Response.write "No records found for the given value. Please try again."
    Else
    	if ex = "greater or equals to" then
    	sql3 = "Select Count(*) as ct from ABSSubGrade where Grade >= " &v& " AND ABSID = "&rs1("ABSID")&""
    	
    	elseif ex = "equals to" then
    	sql3 = "Select Count(*) as ct from ABSSubGrade where Grade = " &v& " AND ABSID = "&rs1("ABSID")&""
    	
    	else
    	sql3 = "Select Count(*) as ct from ABSSubGrade where Grade <= " &v& " AND ABSID = "&rs1("ABSID")&""
    	end if
    	
    	Set RS3 = Server.CreateObject("ADODB.RecordSet")
    	RS3.open sql3,conn
    
    	Dim counter 
    	counter = 0 
    	If not rs3.eof then
    	counter = RS3("ct")
    	End if 
    	
    %>
        <br>
        <br>
        <table width="550" border="1" cellpadding="7" cellspacing="1">
          <tr> 
            <td colspan="2" class="small"><font color="#000000" size="4"><font face="Arial, Helvetica, sans-serif">Modified 
              ABS</font></font><font color="#000066" size="3" face="Arial, Helvetica, sans-serif"><strong> 
              <input name="item" type="hidden" id="item" value= "<%=Request("item")%>">
              </strong></font></td>
          </tr>
          <tr> 
            <td width="213" class="small"><%=i%></td>
            <%
    rwset=1
    ct=1
    Do while not rsc.eof
    %>
            <td width="300" class="small"><font color="#000000"> 
              <div align="center"> <u><strong><%=RSC("GradeName")%></strong></u> 
                <input name="opt" type="checkbox" id="opt" value="<%=rsc("Item")%>">
              </div>
    		  <div align="center">
              <table width="101" border="1" cellspacing="0" cellpadding="4">
                <tr> 
                  <td width="89" height="46"><div align="center"><%=RSC("item")%> 
                    </div></td>
                </tr>
                <tr> 
                  <td width="89" height="56"><div align="center"><%=RSC("Grade")%> 
                    </div></td>
                </tr>
              </table>
              </div>
              <%
    f=rwset mod (ct+1)
    if(f=0) then
    %>
              </font> <font color="#000000">&nbsp; </font> </td>
          </tr>
          <%
    end if
    ct=ct+1
    rwset=rwset+1
    
    rsc.movenext
    loop
    end if
    end if
    %>
        </table>
        <br>
        <input name="Go" type="submit" id="Go" value="Go">
        <br>
        <br>
        <br>
        <%
    	Response.write "Your search returns "
    	Response.write cint(counter)
    	Response.write " records."
    	'response.write sql3
    	'response.write sqlC
    %>
        </strong></font> </div>
    <input name="opt" type="checkbox" id="opt" value="<%=RSC("Item")%>">

  2. #2
    Join Date
    Feb 2003
    Posts
    2,745
    might be easier if this
    Code:
    <input name="opt" type="checkbox" id="opt" value="<%=RSC("Item")%>">
    was
    Code:
    <input name="opt=<%=RSC("Item")%>" type="checkbox" id="opt">
    then you could loop thru like this
    Code:
    Dim f
    Dim optValue
    
    For Each f in Request.Form
       If len(f) > 2 Then
          If Lcase(Left(f, 3)) = "opt" Then '' we found a checkbox
             optValue = Right(f, len(f)-3)
             '' now u can do something with the value
          End If
       End If
    Next

  3. #3
    Join Date
    Jan 2005
    Posts
    80
    but the check box has to have a value which is retrieve from the database. if i will to change it, will it affect the program? any alternative?

  4. #4
    Join Date
    Feb 2003
    Posts
    2,745
    Code:
    <input name="opt<%=RSC("Item")%>" type="checkbox" id="opt" value="<%=RSC("Item")%>">
    u can still loop thru like i showed b4

    also there was a typo in my earlier post: notice the change

    name="opt<%=RSC("Item")%>"

  5. #5
    Join Date
    Jan 2005
    Posts
    80
    how do i do the javascript validation?like that?

    Code:
    if ( (document.form1.opt[0].checked != true) && 
         (document.form1.opt[1].checked != true) && 
         (document.form1.opt[2].checked != true) ) 
    {
     	alert("Please select at least one grade before submitting the form."); 
    	return false;
    }

  6. #6
    Join Date
    Feb 2003
    Posts
    2,745
    yep, if u already know the values to expect from the recordset, but if not, let the asp generate the javascript

  7. #7
    Join Date
    Jan 2005
    Posts
    80

    Thumbs up

    excuse me, what do u mean by let the ASP generate the Javascript? i do not know the values for the recordset so how should i go about doing it? please enlighten me. thanks a million.

  8. #8
    Join Date
    Feb 2003
    Posts
    2,745
    something along the lines of this
    Code:
    <%
    ar = array(1,2,3,4) '' really from a db but this'll work for demo purposes
    %>
    
    <script><!--
    ar = new Array();
    <%
    For i = 0 to ubound(ar)
       Response.Write "ar[" & i & "]=" & ar(i) & ";" & vbCrLf
    Next
    %>
    // -->
    </script>
    this could be cleaner, but should serve as an example...i hope

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles