Required Fields if Checkbox Selected
Is there a way that I can make a text field or text fields required if a particular checkbox from a group of checkboxes is selected?
Is there a way that I can make a text field or text fields required if a particular checkbox from a group of checkboxes is selected?
Takes a little hand coding but conditional validation can be done, see the following post by Ray for details:
showpost.php?p=3355&postcount=4
Hi Jason
That mostly makes sense to me, however I am using a radio button group and I would like the validation to work if a specific radio button is checked.
The way Form Builder has made the group means that all of them have the same name but a different id.
ok, using rays example of the phone number field
you could update that to compare the call me field to a specific value:
<?php echo(((isset($_POST['callMe']) && $_POST['callMe'] = "yes")?$_POST['phoneNumber']:"(222) 222-2222")); ?>
I have tried replicating the code that you have suggested based on the code generated by Form Builder.
I have ended up with
<?php echo((isset($_GET["EmploymentStatus"]) && $_GET['EmploymentStatus'] = "Employed")?ValidatedField("ApplicationForm","EmployersName"):"")); ?>
However this produces a Syntax error in dreamweaver.
I wasnt sure what I would use for the validation part, you used a telephone number example, as this is just a required field
Code for the Radio Button
<input type="radio" name="EmploymentStatus" id="EmploymentStatus__1" value="Employed" class="formRadioField_Standard" <?php if (!(strcmp((isset($_GET["invalid"])?ValidatedField("ApplicationForm","EmploymentStatus"):""),"Employed"))) {echo "checked=\"checked\"";} ?> tabindex="19">
Code for the Form Field that only requires validation if the above button is checked
<input id="EmployersName" name="EmployersName" type="text" value="<?php echo((isset($_GET["invalid"])?ValidatedField("ApplicationForm","EmployersName"):"")); ?>" class="formTextfield_Large" tabindex="24" onBlur="hideServerError('EmployersName_ServerError');">
Also I am unsure as to why everything is $_GET when the form was created as $_POST and also submits using $_POST
I dont believe you aere editing the correct code.
it looks like you editing the value of the form element and not the server validations
send a copy of the page with out the change, and i can tell you what code needs to be edited.
I have attached the page as requested.
I also get a Syntax error for one of the Spry Validations but have found that if I manually change the code as the names dont match then it is ok. I am unsure why this is happening as this is generated code.
Thank you Jason!
ok, so the required validation for the employers name is at line 22:
$WAFV_Errors .= WAValidateRQ((isset($_POST["EmployersName"])?$_POST["EmployersName"]:"") . "",true,12);
to make that required only if the Employment status is set to employed, change that to:
$WAFV_Errors .= WAValidateRQ(((isset($_POST["EmploymentStatus"]) && $_POST["EmploymentStatus"] == "Employed")?$_POST["EmployersName"]:"") . "NA",true,12);
as for the spry validation, which one is giving the syntax error?
one issue i see in your form is that you have some form element names that are not valid.
for exaample the declaration select lists use numbers and the underscore character as the first character in the name. When naming form elements, you should follow these rules:
1) only use letters and numbers, the only exception is the underscore.
2) The first character of a form element name must be a letter.
NOTE: You will need to delete the following line for spry validation, spry does not support conditional validation:
var EmployersName_Spry = new Spry.Widget.ValidationTextField("EmployersName_Spry", "none",{validateOn:["blur"]});
Thank you for the comment about the numbering issue. It was just because it was question 1, question 2 etc.
I have change this and it has sorted the Syntax error that was occuring
Yeah, spaces cant be used in form element names either, instead of "question 1" for the name, you should use "question_1" or "question1"
Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.
These out-of-the-box solutions provide you proven, tested applications that can be up and running now. Build a store, a gallery, or a web-based email solution.