Disabled TextBox If CheckBox is not selected.

Hello, I have an ASP page that dynamically generates form with checkboxes(products) and textboxes(quantity).
What I need to do it to integrate Javascript into the following source code so it will disable the input of quantity if the product is not selected(checked).
Therefore, if there are 4 products retrieved from database. They will be 4 checkboxes displayed along with their corresponding Quantity textbox. If the 1st product is not checked, its quantity will be disabled. Any idea on this?



<%

DIM DBRead
Set conn = Server.CreateObject("ADODB.CONNECTION")
Set RS = Server.CreateObject("ADODB.RECORDSET")
conn.open "EPCC
SQL = "Select products from customer where username='inti'"
RS.open SQL,conn
Rec = RS("products")
Rec = replace(Rec,".","")

DBRead = split(Rec,",")
Response.Write "<form onsubmit=return checkform(this) method=post action=QuatProc.asp>"
For Each DBR in DBRead
   Response.Write "<input type=Checkbox Name=Products value="&DBR&">" & DBR & " " & "<input type=text name=Qty size=4 maxlength=4>" & "<br>"
Next
Response.Write "<p><input type=submit name=Submit value=Submit></p>"
Response.Write "</form>"
RS.close
set DBRead = nothing
set rs = nothing
conn.close
set conn=nothing
%>
LVL 2
sherlyAsked:
Who is Participating?
 
nimaigConnect With a Mentor Commented:
Check this ... it is quite similar to what you want ...


<html>

<head>
<SCRIPT LANGUAGE=javascript>
<!--
      /** This method is only to disable textbox for Netscape **/
      function checkDisabled(obj,checkObjName){
            if(document.layers){
                  var makeDisable = true;
                  varCheckObj = eval("document.forms[0]." + checkObjName);
                  if(varCheckObj.checked)
                        makeDisable = false;
                  if(makeDisable)
                        obj.blur();
            }
      }
      
      /** This method is to enable and disable textbox in IE and populate value **/
      function enableText(checkObj, textObjName){
            var textObj = eval("document.forms[0]." + textObjName);
            var txtValue = trim(textObj.value);
            if(document.all){
                  textObj.disabled = !checkObj.checked;
                  if(checkObj.checked && (txtValue == null || txtValue == ""))
                        textObj.value = 1;
            }else{
                  if(checkObj.checked && (txtValue == null || txtValue == ""))
                        textObj.value = 1;
            }
      }
      
      /** This method validates the form **/
      function validate(formObj){
            var checkCount = 0;
            for(var i=1; i <= 5; i++){
                  var checkObj = eval("formObj.check" + i);
                  var textObj = eval("formObj.qty" + i);
                  var txtValue = trim(textObj.value);
                  if(checkObj.checked && (txtValue == null || txtValue == "" || txtValue == 0 || isNaN(txtValue))){
                        alert("Please enter valid qty");
                        textObj.focus();
                        textObj.select();
                        return false;
                  }      
                  if(checkObj.checked)
                        checkCount++;
            }
            if(checkCount <= 0){
                  alert("Minimum one product is required");
                  return false;
            }
            return true;
                  
      }
      
      /** This method is to trim a string from right **/
      function RTrim(str){
            var whitespace = new String(" \t\n\r");
        var s = new String(str);
        if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
              var i = s.length - 1;
            while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
                  i--;
                s = s.substring(0, i+1);
            }
        return s;
    }

      /** This method is to trim a string from left **/
    function LTrim(str){
        var whitespace = new String(" \t\n\r");
        var s = new String(str);
        if (whitespace.indexOf(s.charAt(0)) != -1) {
              var j=0, i = s.length;
            while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
                  j++;
            s = s.substring(j, i);
        }
            return s;
      }

      /** This method is to trim a string from both left and right **/
      function trim(str){
            return RTrim(LTrim(str));
    }
//-->
</SCRIPT>

</head>

<body>
<form onsubmit="return validate(this)" method="post" action="second_page.asp">
<table bgcolor="#CCCCCC">
      <tr>
            <td></td>
            <td align="center">Product</td>
            <td align="center">Qty</td>
      </tr>
      <tr bgcolor="#FFFFFF">
            <td><input type="checkbox" name="check1" value="1" onclick="enableText(this,'qty1')"></td>
            <td>Product1</td>
            <td><input type="text" name="qty1" disabled onFocus="checkDisabled(this,'check1')"></td>
      </tr>
      <tr  bgcolor="#FFFFFF">
            <td><input type="checkbox" name="check2" value="1" onclick="enableText(this,'qty2')"></td>
            <td>Product1</td>
            <td><input type="text" name="qty2" disabled onFocus="checkDisabled(this,'check2')"></td>
      </tr>
      <tr  bgcolor="#FFFFFF">
            <td><input type="checkbox" name="check3" value="1" onclick="enableText(this,'qty3')"></td>
            <td>Product1</td>
            <td><input type="text" name="qty3" disabled onFocus="checkDisabled(this,'check3')"></td>
      </tr>
      <tr  bgcolor="#FFFFFF">
            <td><input type="checkbox" name="check4" value="1" onclick="enableText(this,'qty4')"></td>
            <td>Product1</td>
            <td><input type="text" name="qty4" disabled onFocus="checkDisabled(this,'check4')"></td>
      </tr>
      <tr  bgcolor="#FFFFFF">
            <td><input type="checkbox" name="check5" value="1" onclick="enableText(this,'qty5')"></td>
            <td>Product1</td>
            <td><input type="text" name="qty5" disabled onFocus="checkDisabled(this,'check5')"></td>
      </tr>
      <tr  bgcolor="#FFFFFF">
            <td colspan=3 align="center"><input type="submit" value="submit"></td>
      </tr>
</table>
</form>
</body>

</html>

0
 
Michel PlungjanIT ExpertCommented:
Dim count=0;

for each

Response.write "<input type=Checkbox Name=Products"&count&" value="&DBR&" onClick=\"this.form.qty"&count&".disabled=!this.checked>" & DBR & " " &
"<input type=text name=Qty"&count&" size=4 maxlength=4 disabled onFocus=\"if (this.disabled) this.blur()\">" & "<br>"
count = count + 1
.
.
.
%>
<script>
// Netscape code
for (i=0;i<document.forms[0].elements.length;i++) {
   if (document.forms[0].elements[i].name.indexOf('Qty')!=-1)
      document.forms[0].elements[i].disabled=true;
}
</script>
0
 
Michel PlungjanIT ExpertCommented:
I wish I could write VBScript faster :(

Michel
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
sherlyAuthor Commented:
How do I integrate this javascript into my existing ASP code?
0
 
nimaigCommented:
try this : U may need to work out for escape character


dim i = 0
For Each DBR in DBRead
  i++
  Response.Write "<input type=Checkbox Name=Products" & i & " onclick='enableText(this,\'Qty" & i & "\')' value="&DBR&">" & DBR & " " & "<input type=text
name=Qty" & i & " size=4 maxlength=4 disabled onFocus='checkDisabled(this,\'Products" & i & "\')'>" & "<br>"
Next
0
 
Michel PlungjanIT ExpertCommented:
You simply change your response.write
with the response.write code I posted after adding a counter

The asp possibly needs debugging - I am not a vbscripter.

Then AFTER the %> you paste the script I wrote.

Michel
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.