Web Analytics Made Easy -
StatCounter Checkbox Required - CodingForum

Announcement

Collapse
No announcement yet.

Checkbox Required

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Checkbox Required

    Is there a way to make a field required based on whether or not a checkbox is marked? I have a text field that appears when a checkbox is marked. I would like that text field to be required when it is visible. Is this possible? I am using Acrobat Professional 8.0 to create the form.

  • #2
    Pseudo code:-

    if (the checkbox is checked) { //and hence the textfield is visible
    if (document.getElementById("textfieldid").value == "": // empty
    alert (You must enter something into the textfield");
    return false;
    }
    }

    But of course value == "" is poor validation as even a single space or a ? will return false (i.e. pass the test). You need to strip leading spaces (and perhaps numbers and special characters) and then set a suitable minimum length for the entered text.



    "Normally the Pakistani police are armed with rifles and atomic weapons". - Political commentator.
    Last edited by Philip M; Apr 2, 2009, 04:06 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

    Comment


    • #3
      Try this ...

      Assuming you have HTML that looks something like this:
      Code:
      <input type="checkbox" id="CBox"
       onclick="vis='none';if(this.checked) {vis='block'};document.getElementById('Comments').style.display=vis" >Comments<br>
      <textarea id="Comments" style="display:none"></textarea>
      In your validation section add:
      Code:
      if ((document.getElementById('CBox').checked==true)
        &&  (document.getElementById('Comments').value == ''))
        { alert('Comments required when checked'); return false; }
      // ... other validation
      return true; // if all else OK
      Note: code not tested, but should work.

      Man that 'PhilliM' is a fast typist!!!
      Last edited by jmrker; Apr 2, 2009, 04:06 PM. Reason: PhillipM beat me to it again!!!

      Comment


      • #4
        Small typo in code above:-

        && (document.getElementById('Comments').value == ''))

        Originally posted by LadyDi View Post
        Is there a way to make a field required based on whether or not a checkbox is marked? I have a text field that appears when a checkbox is marked. I would like that text field to be required when it is visible. Is this possible? I am using Acrobat Professional 8.0 to create the form.
        A small point but what if the user enters a comment and then unchecks the checkbox? It might be an idea to cause the checkbox to remain checked whenever there is any text in the comments field.

        if (document.getElementById('Comments').value != "")) {
        ((document.getElementById('CBox').checked = true);
        }

        All the code given in this post has been tested and is intended to address the question asked.
        Unless stated otherwise it is not just a demonstration.

        Comment


        • #5
          This is the code I put in.

          if (Unit_Down == "Yes") { //and hence the textfield is visible
          if && (document.getElementById('UnitDownEx').value == ''))
          alert ("You must enter something into the textfield");
          return false;
          }
          }

          Unit_Down is my checkbox. "Yes" is the export value of that box. UnitDownEx is my text field. I keep getting an error message that reads "Missing ( before condition" and it highlights the line that begins "alert". What have I done wrong?

          Comment


          • #6
            if (Unit_Down.value == "Yes") { //and hence the textfield is visible
            if && (document.getElementById('UnitDownEx').value == ''))

            All the code given in this post has been tested and is intended to address the question asked.
            Unless stated otherwise it is not just a demonstration.

            Comment

            Working...
            X