Link to home
Start Free TrialLog in
Avatar of tomfulton
tomfultonFlag for United States of America

asked on

Validate a checkbox and corresponding text box.

I have a form with multiple checkboxes. the last checkbox has a corresponding textbox for a write in value.
I would like to make sure that the textbox can only be filled in if the corresponding checkbox is checked and if that same checkbox is then unchecked then the textbox is cleared out.

<form name="userlist" method="post"> 
<input name="user1" type="checkbox">User1
<input name="user2" type="checkbox">User2
<input name="1writein" type="checkbox">><input type="text" name="1wi" value="">
<input type="submit" name="submit">
</form>

Open in new window

Avatar of HonorGod
HonorGod
Flag of United States of America image

Like this?
<html>
<head>
<title> Associated field </title>
<script language='JavaScript' type='text/javascript'>
  function checkIt( obj, id ) {
    var field   = document.getElementById( id );
    var checked = obj.checked;
    if ( field ) {
      if ( checked ) {
        if ( field.value != '' ) {
          alert( 'specified value: "' + field.value + '"' );
        }
      } else {
        field.value = '';
      }
    } else {
      alert( 'Specified field not found.  id="' + id + '"' );
    }
  }
</script>
</head>
 
<body>
<form name="userlist" method="post" action=''> 
  <input name="user1" type="checkbox">User1
  <input name="user2" type="checkbox">User2
  <input name="1writein" type="checkbox" onclick='checkIt(this,"1wi");'><input type="text" name="1wi" id="1wi" value="">
  <input type="submit" name="submit">
</form>
 
</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kevp75
kevp75
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
yeah, that's what I was attempting, but I have a typo
<%
dim frmField : frmField = request.querystring("1writein")
dim frmField2 : frmField2 = request.querystring("1wi")
if not isnull(frmField) or not isempty(frmField) or not(frmField = "") then
     if not isnull(frmField2) or not isempty(frmField2) or not(frmField2 = "") then
          'process the form here
     else
          'don't process it, throw a message at the user if you need to, etc
     end if
end if
%>

Open in new window

Avatar of tomfulton

ASKER

Is this code from the last submission:
<%
dim frmField : frmField = request.querystring("1writein")
dim frmField2 : frmField2 = request.querystring("1wi")
if not isnull(frmField) or not isempty(frmField) or not(frmField = "") then
     if not isnull(frmField2) or not isempty(frmField2) or not(frmField2 = "") then
          'process the form here
     else
          'don't process it, throw a message at the user if you need to, etc
     end if
end if
%>

something I will put on the same page as the code for the form?  I wanted to do it on the client side if I could so I could catch the blank textbox and checked checkbox  before it is submitted.


Thank you
The first comment does exactly that.  It is on the client side, and demonstrates how to check not only the value of the radiobutton, but also clear the text field should the last radtiobutton be unchecked.

https://www.experts-exchange.com/questions/22989078/Validate-a-checkbox-and-corresponding-text-box.html#20369410
Avatar of sybe
sybe

You probably want to do it both on client and on server side. Client side only works if people have javascript turned on, and no greasemonkey script running to overrule your script.

And yes, the first comment posted does the client side thing. Although it's a bit bit longer then what I suggested.
sybe's correct (as previously stated...)

You would put that code on whatever page that is processing the form.  If your <form action is another page, it would go on that....unless you are doing a post-back in which case you would put it on the page the form is on...
HonorGod:
I put that code in an asp page by itself and my javascript is on but no alert came up and the textbox does not clear if the corresponding checkbox is unchecked.

so I looked into the server side possibility and

sybe:
Do I put your code in the same page as my form itself?
I don't know if I worded the problem different than what I need but in a set of checkboxes a user can select more than one choice but if the user wants the write-in then I want to verify that Both the write-in textbox and its corresponding checkbox is checked.

Thank you for looking at this
Strange.  I'm not familiar enough with asp so as to be able to understand why this would be the case.  If you add an alert to the function to prove that it is getting called, do you see it's output?

For example:


  function checkIt( obj, id ) {
    alert( 'checkIt()' );
    var field   = document.getElementById( id );
...

Open in new window

what I posted for you will do the server side you need
Since the client side didn't work at first, I went with the server side solutions.
Thanks for the assistance.