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.
Announcement
Collapse
No announcement yet.
Checkbox Required
Collapse
X
-
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.
-
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>
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
Man that 'PhilliM' is a fast typist!!!
Comment
-
Small typo in code above:-
&& (document.getElementById('Comments').value == ''))
Originally posted by LadyDi View PostIs 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.
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
-
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
-
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
Comment