Copy link to clipboard
Copied
I have a form where input is determined by radio buttons. If the first one is selected, a certain group of input fields popup for entry. If the second button is selected, another group popsup for entry, etc...
The first group has a field that I want to make required, so I have
<cfinput type="text" name="comments_#id#" required="yes" message="Please enter">
This works. But when I click the second button to get the other group, the comments required message still popsup.
Is there a way to disregard the requirement if I am working with the second group of fields ?
Copy link to clipboard
Copied
I would say, remove the attribute required="yes" and roll your own validation for that field. An illustration of what I mean:
<script type="text/javascript">
function requiredOrNo () {
var isTestVarRequired = false;
for (var i=0; i < document.myForm.check.length; i++) {
/*My validation rule: if you make Choice 1, then testVar cannot be blank*/
if (document.myForm.check.checked && document.myForm.check.value=='c1' && document.myForm.testVar.value==''){
isTestVarRequired = true;
alert('testVar is required');
}
}
if (isTestVarRequired) {
return false;
}
return true;
}
</script>
<cfif isdefined("form.sbmt")>
<cfdump var="#form#">
</cfif>
<cfform action="#cgi.SCRIPT_NAME#" name="myForm" onSubmit="return requiredOrNo();" >
<cfinput type="radio" name="check" value="c1">choice1<br>
<cfinput type="radio" name="check" value="c2">choice2<br>
<cfinput type="text" name="testVar">test var<br><br>
<cfinput type="submit" name="sbmt" value="test it">
</cfform>
Copy link to clipboard
Copied
When I check the first radio button, value=c1, I have a checkbox and input field, comments : <cfinput type="text" name="comments/#xxx#/#yyyy#">
I currently use this to validate the checkbox must be checked :
<script language="JavaScript" >
function validateForm()
{
var result=loopForm(editForm)
if (result =="1")
{
alert("Please check the box.")
return false
}
if (result) {
return false;
}
return true
}
</script>
Using your code to check that the box is checked,would it be if (document.myForm.check.checked == false ???
and to check that the input field is not blank, document.myForm.comments/#xxx#/#yyy#.value=='' ?? The field name=comments/#xxx#/#yyy# is throwing me off.
So I need to check that the checkbox is checked and the input field is not blank.
Copy link to clipboard
Copied
1) You have to give your form the name myForm
2) I would use an underscore( _ ) in field names, in place of /
3) To test the script I gave you, simply replace my field testVar with your comment field
4) Don't forget to use cfoutput within the script tag, for example <cfoutput>document.myForm.comments/#xxx#/#yyy#.value=='' </cfoutput>
Copy link to clipboard
Copied
It has just occurred to me how familiar your code is. I have checked and found that you have posted at least a dozen times on this subject within the past week.
I see that you tend to continue piling up the questions, one after another. It would help if you would at least acknowledge the suggestions that have worked.