Solved

Disabled TextBox If CheckBox is not selected.

Posted on 2001-06-27
6
348 Views
Last Modified: 2012-06-27
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
%>
0
Comment
Question by:sherly
  • 3
  • 2
6 Comments
 
LVL 9

Accepted Solution

by:
nimaig earned 50 total points
ID: 6230603
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 6230608
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 6230610
I wish I could write VBScript faster :(

Michel
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Author Comment

by:sherly
ID: 6230617
How do I integrate this javascript into my existing ASP code?
0
 
LVL 9

Expert Comment

by:nimaig
ID: 6230646
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 6230859
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now