Solved

Disabled TextBox If CheckBox is not selected.

Posted on 2001-06-27
6
357 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What kind of script/language created this graph? 6 67
isNaN issue on the Table input text 2 42
Natural height and jquery 6 40
Json and ajax javascript 24 25
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Suggested Courses

751 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